How To Rename Every File in all Subdirectories in Windows CMD


Suppose we’re in current_dir and want to rename our files within subdirectories.

📂current_dir                      📂current_dir
 ┣ 📜README.md                      ┣ 📜README.md
 ┣ 📜file01.txt                     ┣ 📜file01.md
 ┗ 📂folder1                        ┗ 📂folder1
    ┣ 📜file11.txt         ==>         ┣ 📜file11.md
    ┣ 📜file12.txt                     ┣ 📜file12.md
    ┗ 📂subfolder1                     ┗ 📂subfolder1
       ┣ 📜file111.txt                    ┣ 📜file111.md
       ┗ 📜file112.txt                    ┗ 📜file112.md

Running ren *.txt *.md will convert all our .txt files into .md files, but only in the current directory.

It ignores any .txt files inside subfolders of the current directory.

In order to access every subdirectory, we can use a for loop with a recursive switch.

for /R %x in (*.txt) do ren "%x" *.md

Let’s quickly go over what this means.

for /R will loop through every file and recurse through all subfolders.

%x holds the file name (including file path) of those files that meet the specified condition.

in (*.txt) is this condition that grabs all files that end in .txt.

do ren "%x" *.md runs the ren (rename) command on the file provided by the for loop, converting the .txt to .md.

for /R %x in (files_to_rename) do ren "%x" changed_name