944,057 Members | Top Members by Rank

Ad:
Jun 8th, 2007
0

Recursive file rename

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pink_zippy_123 is offline Offline
6 posts
since Apr 2007
Jun 8th, 2007
0

Re: Recursive file rename

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
mittelgeek is offline Offline
37 posts
since Aug 2005
Jun 10th, 2007
0

Re: Recursive file rename

Click to Expand / Collapse  Quote originally posted by mittelgeek ...

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
Reputation Points: 22
Solved Threads: 19
Posting Whiz
DenisOxon is offline Offline
345 posts
since Jan 2007
Jun 11th, 2007
0

Re: Recursive file rename

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
mittelgeek is offline Offline
37 posts
since Aug 2005
Jun 11th, 2007
0

Re: Recursive file rename

Thanks a lot for both of your help!

Mike
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pink_zippy_123 is offline Offline
6 posts
since Apr 2007
Aug 20th, 2007
0

Re: Recursive file rename

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ashank is offline Offline
1 posts
since Aug 2007
Aug 21st, 2007
0

Re: Recursive file rename

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).
Reputation Points: 10
Solved Threads: 0
Light Poster
mittelgeek is offline Offline
37 posts
since Aug 2005
Mar 1st, 2010
0
Re: Recursive file rename
You can use:

dir *.tsql /b > ren *.sql

Ready!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
daniel.bonetti is offline Offline
3 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Legacy and Other Languages Forum Timeline: using tables in RPGiv
Next Thread in Legacy and Other Languages Forum Timeline: desktop icon in inno setup





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC