Solved

Looping successfully through find and rename routine

Posted on 2013-11-27
8
341 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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:100questions
Comment Utility
Thanks Bill. Move would be fine actually.
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
Thanks Bill, seems to work.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Welcome, glad that helped.

~bp
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
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…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now