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.


9 Years
Discussion Span
Last Post by daniel.bonetti

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

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:

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:


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.


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 ?



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.


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:

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).

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.