Recursive file rename

Reply

Join Date: Apr 2007
Posts: 6
Reputation: pink_zippy_123 is an unknown quantity at this point 
Solved Threads: 0
pink_zippy_123 pink_zippy_123 is offline Offline
Newbie Poster

Recursive file rename

 
0
  #1
Jun 8th, 2007
Hi,

I'm attempting to rename .tql files within a directory tree to .sql. I'm currently trying to use this in windows command line:

 for /R %f in (*.*) do REN *.tql *.sql
this for some reason i can't work out, it is not recursively going down the directory tree and executing the command to rename the files in each directory.

Any ideas??

Thanks in advance.

Mike
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 36
Reputation: mittelgeek is an unknown quantity at this point 
Solved Threads: 0
mittelgeek's Avatar
mittelgeek mittelgeek is offline Offline
Light Poster

Re: Recursive file rename

 
0
  #2
Jun 8th, 2007
For the dataset:

C:\DOCS>dir
Volume in drive C has no label.
Volume Serial Number is xxxx-xxxx

Directory of C:\DOCS

05/29/2007 11:12 AM <DIR> .
05/29/2007 11:12 AM <DIR> ..
06/07/2007 11:45 AM <DIR> Business
06/07/2007 09:33 PM <DIR> My Downloads
05/29/2007 11:12 AM 111 ping-subnet.txt
04/27/2000 04:36 PM 766 UserGuide.ico
08/05/2002 11:21 AM 3,877,898 userguide.pdf


Applying the /b switch provides:

C:\DOCS>dir /b
Business
My Downloads
ping-subnet.txt
UserGuide.ico
userguide.pdf


Providing the following command at the command prompt [remember in a batch file the for statement replaceable variables must use double percent signs, e.g., %%g]:

C:\DOCS>for /f "usebackq tokens=1,2 delims=." %g in (`dir /b .\*.*`) do @echo %g %h

Will provide you with the following output:

Business
My Downloads
ping-subnet txt
UserGuide ico
userguide pdf


Notice that the "." is missing. That is because the delims modifier has parsed the line using "." as the delimiter for tokens. On caveat for using this method is that a file name like:

ping-subnet.txt.cmd

will break this. Not a hard break, but it will cause unexpected behavior. The output for that filename would be:

ping-subnet txt

As you can see, since we only specified that tokens one and two be parsed the third was dropped. So in conclusion, you would probably need a line like this:

for /f "usebackq tokens=1,2 delims=." %g in (`dir /b .\*.*`) do @ren %g.%h %g.sql

Additionally, when using the above command, make sure that you are in the directory where the files to be renamed are located. The rename [ren] command could be substituted with the move command.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 319
Reputation: DenisOxon is an unknown quantity at this point 
Solved Threads: 15
DenisOxon's Avatar
DenisOxon DenisOxon is offline Offline
Posting Whiz

Re: Recursive file rename

 
0
  #3
Jun 10th, 2007
Originally Posted by mittelgeek View Post

for /f "usebackq tokens=1,2 delims=." %g in (`dir /b .\*.*`) do @ren %g.%h %g.sql

Hi Mittelgeek,

Should the dir /b have a /s also to get subdirectories ?

Also should *.* be *.tql as pink_zippy only wishes to ren .tql files ?

Denis
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 36
Reputation: mittelgeek is an unknown quantity at this point 
Solved Threads: 0
mittelgeek's Avatar
mittelgeek mittelgeek is offline Offline
Light Poster

Re: Recursive file rename

 
0
  #4
Jun 11th, 2007
Absolutely. I didn't reread his post before I submitted mine.
for /f "usebackq tokens=1 delims=." %g in (`dir /b /s .\*.tql`) do @ren %g.tql %g.sql
I think that should fix him up.

Thanks for that, DenisOxon. I wouldn't have noticed that if you didn't point it out.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 6
Reputation: pink_zippy_123 is an unknown quantity at this point 
Solved Threads: 0
pink_zippy_123 pink_zippy_123 is offline Offline
Newbie Poster

Re: Recursive file rename

 
0
  #5
Jun 11th, 2007
Thanks a lot for both of your help!

Mike
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1
Reputation: ashank is an unknown quantity at this point 
Solved Threads: 0
ashank ashank is offline Offline
Newbie Poster

Re: Recursive file rename

 
0
  #6
Aug 20th, 2007
Hi
Thanks for your solution. I guess it could lead to problems in case of a file having multiple "." I tried to build on the solution given by you to take care of this multiple dots problem:-

 for /f "usebackq tokens=*" %%g in (`dir /b/s/A:d`) do @cd "%%g" & @ren *.extn1 *.extn2

This would loop through all the sub-directories in a directory (/A:d attribute is there to filter and return only directories), change to that directory and rename all files ending with 'extn1' to file.extn2
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 36
Reputation: mittelgeek is an unknown quantity at this point 
Solved Threads: 0
mittelgeek's Avatar
mittelgeek mittelgeek is offline Offline
Light Poster

Re: Recursive file rename

 
0
  #7
Aug 21st, 2007
Yeah that should work, with a few changes:
for /f "usebackq tokens=*" %%g in (`dir /b /s /A:d`) do @cd "%%g" && @ren *.extn1 *.extn2
But in my original reply to the original post by pink_zippy_123, I stated that this would not work as expected on filenames containing multiple "." in it.
Unfortunately, I got in to teaching mode and went off on a tangent.

After having reread the original post, I think that the ultimate answer to the problem may be something like this for the command line:
for /f "usebackq tokens=*" %h in (`dir /b /A:-D`) do @ren *.extn1 *.extn2 >nul || echo Failure: %h -^> .tql && echo Success: %h -^> .tql) && for /f "usebackq tokens=*" %g in (`dir /b /s /A:D`) do (for /f "usebackq tokens=*" %h in (`dir /b /s /A:-D`) do @ren *.extn1 *.extn2 >nul || echo Failure: %h -^> .tql && echo Success: %h -^> .tql)
Or like this in a batch file:
for /f "usebackq tokens=*" %h in (`dir /b /s /A:-D`) do (
    @ren *.extn1 *.extn2 >nul || echo Failure: %h -^> .tql && echo Success: %h -^> .tql
)
    for /f "usebackq tokens=*" %%g in (`dir /b /s /A:D`) do (
        for /f "usebackq tokens=*" %%h in (`dir /b /s /A:-D`) do (
            @ren *.extn1 *.extn2 >nul || echo Failure: %%h -^> .tql && echo Success: %%h -^> .tql
    )
)
This of course, would traverse all the directories and subdirectories (the second for statement), renaming all files (represented as %%h for the batch file and %h on the command line version) in each of the directories and subdirectories as it goes along (the third for statement).

The first for statement is to get all the files in the current directory because the second for loop will completely miss them; I have not tried this code though, so I invite attempts with results posted to the thread, of course.

P.S. I thnk the batch file (cmd script) method wil be preferable as it makes the code more readable not to metion that I don't think most people will be able to type all that onto the command line without mistyping something. Just look at me; I carefully replied to the post early on and I still missed something (thanx DenisOxon).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Legacy and Other Languages Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC