How to rename multiple files in subfolders using Windows command prompt

This entry was posted on

  • snippet
  • project updates
  • website migration

Problem

As part of the migration from Gatsby to Statiq, I had to rename all the files with the extension mdx to md. Now, I could do it one by one or find a utility but using a single command is more elegant.

Solution

To rename all the files recursively, we can use the REN (rename) command as follows.

FOR /R %G IN (*.mdx) DO REN "%f" "%~dpnG.md"

Let’s unpack this command.

  • FOR: Loops through files
  • /R: Recurse through subfolders.
  • %G: A parameter set to a different value for each iteration of the for loop.
  • (*.mdx): The filename pattern that we want to match.
  • REN: The command to rename a file
  • %~dpnG”: Expands the parameter of the original filename to the fully qualified path without the extension

References:

More like this

Ankur Sheel © 2021
Connect with me
GithubTwitterLinkedIn