How to rename files in Windows CMD (command prompt)
Categories:
Mastering File Renaming in Windows Command Prompt (CMD)

Learn how to efficiently rename single files, multiple files, and files with specific patterns using various commands in the Windows Command Prompt.
Renaming files is a fundamental task in any operating system. While Windows Explorer provides a graphical interface for this, the Command Prompt (CMD) offers powerful, scriptable, and often faster ways to rename files, especially when dealing with multiple files or complex renaming patterns. This article will guide you through the essential CMD commands for renaming files, from basic single-file operations to advanced batch renaming techniques.
The REN
(Rename) Command: Your Basic Tool
The REN
command (or its full form, RENAME
) is the primary utility for renaming files and directories in CMD. It's straightforward and ideal for one-off renaming tasks. The basic syntax requires the current name of the file or directory and its new name. You can also use wildcards for simple batch renaming.
REN "old_filename.txt" "new_filename.txt"
REN "C:\Path\To\Old Folder" "New Folder Name"
Basic usage of the REN
command for files and folders.
REN "My Old File.txt" "My New File.txt"
.Batch Renaming with Wildcards
The REN
command supports wildcards (*
and ?
) which allows you to rename multiple files that follow a certain pattern. The asterisk (*
) matches any sequence of characters, while the question mark (?
) matches any single character. This is incredibly useful for tasks like changing file extensions or adding prefixes/suffixes.
REM Change all .txt files to .log files
REN *.txt *.log
REM Add 'old_' prefix to all .jpg files
REN *.jpg old_*.jpg
REM Rename files like 'file1.doc', 'file2.doc' to 'document1.doc', 'document2.doc'
REN file?.doc document?.doc
Examples of using wildcards with the REN
command for batch renaming.
REN
. There is no undo function in CMD, and an incorrect wildcard pattern can lead to unintended renaming of many files. Always test on a small, non-critical set of files first.Advanced Renaming with FOR
Loops
For more complex renaming scenarios that go beyond simple wildcard patterns, the FOR
loop command combined with REN
provides immense flexibility. This allows you to iterate through files and apply custom renaming logic. This is particularly useful when you need to extract parts of filenames, add sequential numbers, or perform conditional renaming.
flowchart TD A[Start] --> B{"Need to rename multiple files?"} B -->|No| C[Use REN "old" "new"] B -->|Yes| D{"Simple pattern change (e.g., extension)?"} D -->|Yes| E[Use REN *.ext1 *.ext2] D -->|No| F{"Complex logic (e.g., add sequence, extract parts)?"} F -->|Yes| G[Use FOR loop with REN] F -->|No| H[Consider PowerShell or third-party tools] C --> I[End] E --> I G --> I H --> I
Decision flow for choosing the right file renaming method in CMD.
REM Add a sequential number to all .jpg files in the current directory
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR %%f IN (*.jpg) DO (
REN "%%f" "image_!count!.jpg"
SET /A count+=1
)
ENDLOCAL
REM Rename files by replacing a specific string
FOR %%f IN (*.txt) DO (
SET "filename=%%f"
SET "newname=!filename:old_text=new_text!"
IF NOT "!filename!"=="!newname!" REN "%%f" "!newname!"
)
Advanced batch renaming using FOR
loops and variable manipulation.
SETLOCAL ENABLEDELAYEDEXPANSION
command is crucial when using variables within FOR
loops that change during each iteration. It allows variables to be expanded at execution time rather than parse time, using !variable!
instead of %variable%
.1. Open Command Prompt
Press Win + R
, type cmd
, and press Enter
. Alternatively, search for 'Command Prompt' in the Start Menu.
2. Navigate to the Directory
Use the CD
(Change Directory) command to go to the folder containing the files you want to rename. For example, CD C:\Users\YourUser\Documents\Photos
.
3. List Files (Optional but Recommended)
Use DIR
to list the files and verify their current names and extensions before proceeding with any renaming operations.
4. Execute Renaming Command
Based on your needs, use REN
with or without wildcards, or a FOR
loop for more complex tasks. Always double-check your command before pressing Enter
.
5. Verify Renaming
After executing the command, use DIR
again to confirm that the files have been renamed as expected.