| | |
Recursive file rename
Please support our Legacy and Other Languages advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2007
Posts: 6
Reputation:
Solved Threads: 0
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:
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
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
Any ideas??
Thanks in advance.
Mike
For the dataset:
C:\DOCS>
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
C:\DOCS>
Business
My Downloads
ping-subnet.txt
UserGuide.ico
userguide.pdf
Providing the following command at the command prompt [remember in a batch file the
C:\DOCS>
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
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:
Additionally, when using the above command, make sure that you are in the directory where the files to be renamed are located. The rename
C:\DOCS>
dirVolume 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 /bBusiness
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 %hWill 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.sqlAdditionally, 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. Absolutely. I didn't reread his post before I submitted mine.
I think that should fix him up.
Thanks for that, DenisOxon. I wouldn't have noticed that if you didn't point it out.
for /f "usebackq tokens=1 delims=." %g in (`dir /b /s .\*.tql`) do @ren %g.tql %g.sql
Thanks for that, DenisOxon. I wouldn't have noticed that if you didn't point it out.
•
•
Join Date: Aug 2007
Posts: 1
Reputation:
Solved Threads: 0
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:-
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
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
Yeah that should work, with a few changes:
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 "
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:
Or like this in a batch file:
This of course, would traverse all the directories and subdirectories (the second
The first
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).
for /f "usebackq tokens=*" %%g in (`dir /b /s /A:d`) do @cd "%%g" && @ren *.extn1 *.extn2
." 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)
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
)
)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).
![]() |
Similar Threads
- Rename Recycle Bin (Windows tips 'n' tweaks)
- how should i append data to file by overwriting the one's already existing? (Java)
- Help with logonui.exe file (Windows NT / 2000 / XP)
- Adding file formats on terminal server (Windows NT / 2000 / XP)
- How to Rename Multiple Files with Windows Explorer (Windows tips 'n' tweaks)
Other Threads in the Legacy and Other Languages Forum
- Previous Thread: Mfc Vc++
- Next Thread: Oracle Help
Views: 7173 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Legacy and Other Languages





