Link to home
Start Free TrialLog in
Avatar of E=mc2
E=mc2Flag for Canada

asked on

Looping successfully through find and rename routine

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
)
Avatar of Bill Prew
Bill Prew

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
Avatar of E=mc2

ASKER

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'?
Avatar of E=mc2

ASKER

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.
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
Avatar of E=mc2

ASKER

Thanks Bill. Move would be fine actually.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of E=mc2

ASKER

Thanks Bill, seems to work.
Welcome, glad that helped.

~bp