Solved

Looping successfully through find and rename routine

Posted on 2013-11-27
8
344 Views
Last Modified: 2013-12-03
If this script finds several instances of "12345678" or "910111213", I would like for it to append a 1,2,3,4...etc  to the file name which is being written.

For instance if it finds several instances of "12345678" in various files, then I need for it to write to FILE.txt, FILE1.txt, FILE2.txt and so forth:

setlocal enabledelayedexpansion

for %%a in ("C:\Users\Desktop\New folder\*.ed") do (

find "12345678" "%%~fa" && ren "%%~fa" FILE.txt
find "910111213" "%%~fa" && ren "%%~fa" OTHER.txt
)
0
Comment
Question by:100questions
  • 4
  • 4
8 Comments
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39683730
Give this a try, I think I understand what you wanted.

@echo off
setlocal enabledelayedexpansion

set FileCount=0
Set FileCount=0

for %%a in ("C:\Users\Desktop\New folder\*.ed") do (
  find "12345678" "%%~fa" && (
    set /A FileCount+=1
    ren "%%~fa" "FILE!FileCount!.txt"
  )
  find "910111213" "%%~fa" && (
    set /A OtherCount+=1
    ren "%%~fa" "OTHER!OtherCount!.txt"
  )
)

Open in new window

~bp
0
 

Author Comment

by:100questions
ID: 39687653
Thanks Bill.  Seems to work, however why do you have to Set FileCounts?

set FileCount=0
Set FileCount=0

What if I added another 5 searches for a total of 7, would I need 7 'set FileCounts'?
0
 

Author Comment

by:100questions
ID: 39687654
Bill.  Lastly, if I want to write the new FILE.txt files in C:\File\ and in new OTHER.txt files in C:\Other, to ask this question would I have to start a new thread or could you also answer it here?

Thanks for your help.
0
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
LVL 53

Expert Comment

by:Bill Prew
ID: 39687669
Sorry,

These:

set FileCount=0
Set FileCount=0

should have been:

set FileCount=0
Set OtherCount=0

and no, you wouldn't need any more than that.  These just start the sequence off for the two types of files we write, and then we increment for each file we output.

So, if you want to move the files to a different folder, do you want to copy, or move?

~bp
0
 

Author Comment

by:100questions
ID: 39687672
Thanks Bill. Move would be fine actually.
0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39687677
Okay, give this a try, I think it handles the folders, but I didn't test it here. I typically like to copy, then delete for these type of renaming moves. If the COPY and / or the DEL are displaying results lines you don't want we can easily suppress those.

Let me know what you are thinking as far as additional searches, there may be approaches that are easier than others.

@echo off
setlocal enabledelayedexpansion

set FileDest=c:\file
set OtherDest=c:\other

set FileCount=0
set OtherCount=0

for %%a in ("C:\Users\Desktop\New folder\*.ed") do (
  find "12345678" "%%~fa" && (
    set /A FileCount+=1
    copy "%%~a" "%FileDest%\FILE!FileCount!.txt"
    del "%%~a"
  )
  find "910111213" "%%~fa" && (
    set /A OtherCount+=1
    copy "%%~a" "%OtherDest%\OTHER!OtherCount!.txt"
    del "%%~a"
  )
)

Open in new window

~bp
0
 

Author Comment

by:100questions
ID: 39694415
Thanks Bill, seems to work.
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39694435
Welcome, glad that helped.

~bp
0

Featured Post

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question