How To Rename Every File in all Subdirectories in Windows CMD

Published Apr 19, 2020  ∙  Updated May 2, 2022

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

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

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