Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Looping successfully through find and rename routine

Posted on 2013-11-27
8
Medium Priority
?
352 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 57

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 57

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 57

Accepted Solution

by:
Bill Prew earned 2000 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 57

Expert Comment

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

~bp
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
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…

704 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