1

Is there a way to change the file extensions of existing files (inside a folder) simultaneously?

I have a folder and within are different subfolders (nested). All the files are .txt, and I want to change them all to .md.

Is there a way to change those simultaneously, or do I really need to modify them one by one? :-(

Thanks, Faye

Additional questions:

@user1016274 : Thanks. By the way, will it also work with files with no extensions? I haven't tried it yet, but I discovered just now that some folders have files with no extension. Pls see the image I added (above), pls see it. Thanks.

enter image description here

Faye
  • 121
  • Run cmd, go to the directory where the files reside, and type ren *.txt *.md. – AFH Dec 01 '17 at 11:46
  • @AFH : Thanks for the reply, but that solution won't be good in my case, because I have multiple deeply nested folder structure. Doing that would mean I have to do it in every single folder, every single subfolder, every folder inside all subfolders, etc. Is there any other solution? Thanks, Faye.. – Faye Dec 01 '17 at 11:50
  • If I'd been at my computer when you responded, I'd have given you user1016274's answer. Note that ren and rename are synonyms. – AFH Dec 01 '17 at 14:01

1 Answers1

2

The rename command allows for a wildcard:
rename *.txt *.md would rename all files in one call, in the current directory.

Now you just need to traverse all directories down from the root dir. For this, there is a for command:
cd /d <rootdir> & for /R %d in (.) do @echo %d

Putting it all together:

cd /d <rootdir>
for /R %d in (.) do @rename "%d\*.txt" *.md
user1016274
  • 1,559
  • Can you pls make it a bit clearer to me. Do I need to type that 'as is', and I just need to change the and put my directory name, ie ie: <E:\Documents\Freelance\Project22> ? Thanks, Faye – Faye Dec 01 '17 at 11:55
  • Yes, exactly. To be safe, you could make a test run with do @dir /b %d\*.txt. You will see an error message for each directory which does not contain any '.txt' file. – user1016274 Dec 01 '17 at 12:00
  • Thanks, I will experiment with that later at home, and will mark this as correct answer if I got it right. Thanks. – Faye Dec 01 '17 at 12:07
  • Please note: I added quotation marks to take care of directories containing spaces in their names. And secondly, the syntax is correct for running the command from a command line, not within a batch file. Within a batch file, you need to double each '%' character. – user1016274 Dec 01 '17 at 12:10
  • Thanks. By the way, will it also work with files with no extensions? I haven't tried it yet, but I discovered just now that some folders have files with no extension. Pls see the image I added (above), pls see it. Thanks. Those are also text files (book stories), I can view them and edit them in VS-Code, but they have no extensions. – Faye Dec 01 '17 at 12:17
  • Yes, leaving out the extension would rename all and every file: rename "%d\*" *.md. – user1016274 Dec 01 '17 at 12:25