Solved

Looping successfully through find and rename routine

Posted on 2013-11-27
8
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 54

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 54

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 54

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 54

Expert Comment

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

~bp
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

An article on effective troubleshooting
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

752 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