Renaming File Extensions Recursively

Experts,

I have a directory with many subdirectories, all of which contain files with a .dta extension.  I'd like to rename the files in the parent directory and all its children to another file extension.

How can I do this?
LVL 1
NigelRocksAsked:
Who is Participating?
 
AmazingTechConnect With a Mentor Commented:
oBdA you're right I sometimes forget to put in the "tokens=* delims="
for /f "Tokens=* Delims=" %%a in ('dir /a-d /s /b "C:\Folder\*.dta"') do echo ren "%%a" "*.txt"

Open in new window

0
 
AmazingTechCommented:
Try this batch file when the output looks correct remove the echo to have it actually rename the files.
for /f %%a in ('dir /a-d /s /b "C:\Folder\*.dta"') do echo ren "%%a" "%%~na.txt"

Open in new window

0
 
AmazingTechCommented:
Or we could just use *.txt

Replace the .txt with the extension you want to change it to.
for /f %%a in ('dir /a-d /s /b "C:\Folder\*.dta"') do echo ren "%%a" "*.txt"
 

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
oBdAConnect With a Mentor Commented:
Sorry, but this will fail with any file or folder containing a space.
That could be fixed with a "delims=", but why is everybody using "for /f" to scan files in subfolders anyway, when there's a native "for" command to do so?

for /r "C:\Test" %a in (*.dta) do ren "%a" "%~na.bla"

/r will *r*ecurse subdirectories, "C:\Test" is the folder to start the search in (current folder will be used if not specified), "*.dta" is the file mask to search for, "%~na" expands "%a" to only the *n*ame. "bla" is whatever you want to use as extension.
The command above is for direct input in the shell. For use in a batch script, just double up on the percent signs:

for /r "C:\Test" %%a in (*.dta) do ren "%%a" "%%~na.bla"
0
 
NigelRocksAuthor Commented:
It didn't work and I didn't get any error messages.  The screen seems to indicate that everything went as planned, but all the file extensions are the same.
Ideas?
 
 
 
0
 
NigelRocksAuthor Commented:
May bad.  It forgot about taking out the ECHO.  Works like a charm.  Thanks, gents.
0
All Courses

From novice to tech pro — start learning today.