How to Rename Multiple Files with Command Prompt (ren, for Loops and Batch Files)
To rename multiple files in Command Prompt, open cmd in the folder and use ren with wildcards for simple changes, such as ren *.txt *.md to change every .txt extension to .md. For anything more, like adding a prefix or numbering files, wrap ren in a for loop: for /f "delims=" %f in ('dir /b /a-d *.jpg') do ren "%f" "trip_%f".
The rest of this guide explains exactly how ren works (its wildcard rules are stranger than most people expect), gives tested patterns for the common jobs, and shows how to preview a rename before it touches anything. Command Prompt has no undo, so the preview step matters.
Open Command Prompt in the Right Folder
ren works on files in the current folder unless you give it a path. The quickest way to start in the right place:
- Open the folder in File Explorer.
- Click the address bar, type
cmdand press Enter.
A Command Prompt window opens already in that folder. Alternatively, open Command Prompt from the Start menu and change folder with cd /d:
cd /d "D:\Photos\Lisbon 2026"The /d switch lets cd change drive as well as folder. Run dir to confirm you are looking at the files you expect.
Important if you use Windows Terminal: on Windows 11, right-clicking a folder and choosing Open in Terminal usually opens PowerShell, not Command Prompt. In PowerShell, ren is an alias for Rename-Item, which does not accept wildcard patterns like *.md as the new name, and the for loops below are cmd syntax. Type cmd and press Enter to switch to Command Prompt inside the same tab, or open a Command Prompt tab from the drop-down.
The ren Command
ren and rename are the same command:
ren [drive:][path]oldname newnameA single file:
ren "IMG_4821.jpg" "lisbon-tram.jpg"Rules worth knowing:
- The new name is a name only. You cannot put a drive or folder in the second argument.
ren C:\a\notes.txt C:\b\notes.txtfails with "The syntax of the command is incorrect."rencannot move files; usemovefor that. - The old name can include a path.
ren "D:\Docs\draft.txt" "final.txt"renames the file in D:\Docs, wherever you currently are. - Quote names with spaces.
ren "my notes.txt" "meeting notes.txt". Without quotes, cmd sees several arguments and fails with a syntax error. - It refuses to overwrite. If the new name already exists, you get "A duplicate file name exists, or the file cannot be found." Nothing is overwritten.
- There is no undo. Ctrl+Z in File Explorer does not reverse a rename done in Command Prompt.
Renaming with Wildcards: What ren Actually Does
ren accepts * and ? in both names. In the first name they select files, as you would expect. In the second name they are a mask that builds each new name from the old one, and that mask works position by position, not like find-and-replace.
Changing extensions: the case that just works
ren *.txt *.md
ren *.jpeg *.jpg
ren *.JPG *.jpg*.md means "keep everything up to the last dot, then use .md". notes.txt becomes notes.md, and archive.2025.txt becomes archive.2025.md. The third line lower-cases uppercase extensions, because the new extension is written exactly as you typed it. This is the most common and most reliable use of ren wildcards. More extension recipes are in how to bulk rename file extensions, and if you are unsure whether changing an extension is safe for your files, read how to change a file extension first: renaming does not convert anything.
Adding a prefix: the case that surprises people
You might expect this to add "trip_" to the front of every JPG:
ren *.jpg trip_*.jpgIt does not. Literal characters in the mask overwrite characters of the old name one for one. beach.jpg becomes trip_.jpg, because the five characters "trip_" replace the five characters "beach", and * then copies whatever is left before the dot, which is nothing. IMG_0412.jpg becomes trip_412.jpg. Some files get mangled, some collide with each other and fail with the duplicate-name error.
The same rule explains question-mark masks:
ren *.jpg ???-*.jpgEach ? copies one character from the old name, and the literal - replaces the fourth character. IMG_1234.jpg becomes IMG-1234.jpg, which is handy, but beach.jpg becomes bea-h.jpg. Masks like this only make sense when every file shares the same structure.
To add text to names reliably, use a for loop, covered next.
Another wildcard quirk: 8.3 short names
On drives where Windows still generates old 8.3 short names, a three-letter extension pattern also matches longer extensions that start with the same letters. dir *.htm can list page.html too, because its short name ends in .HTM. If your folder mixes extensions like this, list the matches with dir /b *.htm before you run a rename.
for Loops: Renaming Files One by One
A for loop runs a command once per file. Inside it, %f holds the current file name, and modifiers give you parts of it:
%~nf: the name without extension (beach)%~xf: the extension with its dot (.jpg)%~nxf: name and extension (beach.jpg)%~ff: the full path
Single % at the prompt, double %% in a .bat file
Commands typed directly at the prompt use one percent sign: %f. In a batch file (.bat or .cmd), every loop variable needs two: %%f, %%~nf and so on. Copy a one-liner into a batch file without doubling them and it fails. All examples below are shown for typing at the prompt unless they are labelled as a batch file.
Add a prefix to every file
The simple version you will see in most guides:
for %f in (*.jpg) do ren "%f" "trip_%f"This often works, but it has a known flaw: the plain for loop reads the folder while it is renaming, and the new names still match *.jpg, so some files can be picked up again and end up as trip_trip_beach.jpg. The reliable version takes a complete list of names first, with dir, and then renames:
for /f "delims=" %f in ('dir /b /a-d *.jpg') do ren "%f" "trip_%f"dir /bprints bare file names,/a-dleaves out folders."delims="keeps each whole line together, so names with spaces stay intact.- The quotes around
"%f"and"trip_%f"handle spaces in names.
Add a suffix before the extension
for /f "delims=" %f in ('dir /b /a-d *.jpg') do ren "%f" "%~nf_edited%~xf"beach.jpg becomes beach_edited.jpg.
Add the date to file names
for /f "delims=" %f in ('dir /b /a-d *.pdf') do ren "%f" "2026-11-08_%f"Type the date you want as plain text. Using cmd's %date% variable is unreliable because its format depends on the region settings of the PC.
Preview First: the echo Dry Run
Put echo in front of ren and the loop prints each command instead of running it:
for /f "delims=" %f in ('dir /b /a-d *.jpg') do @echo ren "%f" "trip_%f"The @ stops cmd from also printing the echo command itself, so you get a clean list:
ren "beach.jpg" "trip_beach.jpg"
ren "IMG_0412.jpg" "trip_IMG_0412.jpg"Check the list, then press the Up arrow, delete @echo and run it for real. It is also worth working on a copy of the folder the first time you run a new pattern.
Number Files in Sequence (Batch File)
Numbering needs a counter that changes inside the loop, which requires delayed expansion. That is much easier in a batch file. Save this as number.bat in the folder and run it:
@echo off
setlocal enabledelayedexpansion
set n=0
for /f "delims=" %%f in ('dir /b /a-d /on *.jpg') do (
set /a n+=1
set "num=000!n!"
ren "%%f" "lisbon_!num:~-3!%%~xf"
)
endlocalHow it works:
setlocal enabledelayedexpansionlets you read the counter with!n!, which is evaluated each time round the loop.%n%would be expanded once, before the loop starts, and every file would get the same number.dir /onsorts the files by name, so the numbering follows name order. Use/odto number by date modified instead.set "num=000!n!"and!num:~-3!keep the last three characters, which zero-pads the number: 001, 002 ... 999. Use more zeros and~-4for four digits.%%~xfkeeps each file's original extension.
The result is lisbon_001.jpg, lisbon_002.jpg and so on. Two caveats:
- If a target name already exists (say you run the script twice),
renrefuses that file with the duplicate-name error and moves on. - With delayed expansion on, exclamation marks in file names are stripped when
%%fis expanded, so files with!in their names fail to rename. Rename those separately.
For other ways to produce numbered names, see renaming files in sequence.
Replace Text in File Names (Batch File)
ren has no find-and-replace, but batch variables do: !name:old=new! replaces every occurrence of old with new. This batch file replaces spaces with underscores in every file in the folder. Save it as fix-spaces.bat (no spaces, so it leaves itself alone) and run it there:
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%f in ('dir /b /a-d') do (
set "name=%%f"
set "new=!name: =_!"
if not "!new!"=="!name!" ren "%%f" "!new!"
)
endlocalChange !name: =_! to !name:draft=final! to swap words, or to !name:copy=! to delete a word. The replacement is not case-sensitive. The if line skips files whose name would not change. The same caveat about ! in names applies.
Rename Files in Subfolders Too
for /r walks the current folder and every folder below it. Changing extensions recursively:
for /r %f in (*.txt) do ren "%f" "%~nf.md"Here %f is a full path, which ren accepts as the old name, and %~nf.md gives a plain new name, which is what ren requires.
For prefixes in subfolders, use dir /s /b so the list is complete before renaming starts, and %~nxf to keep only the file name in the new name:
for /f "delims=" %f in ('dir /s /b /a-d *.jpg') do ren "%f" "trip_%~nxf"Run either one with @echo in front of ren first. A recursive rename in the wrong folder is the hardest mistake to clean up.
If you need to rename the folders themselves rather than the files inside them, see bulk renaming folders on Windows.
When to Use Something Other Than Command Prompt
Command Prompt is fine for extensions, prefixes, suffixes and simple numbering. It gets painful when you need any of these:
- Real find-and-replace with patterns, case changes or regex: PowerShell's
Rename-Itemwith-replacehandles regular expressions and has a built-in-WhatIfpreview. See the PowerShell bulk rename guide. - A visual preview of every new name: PowerRename, part of Microsoft PowerToys, adds a right-click option in File Explorer with search and replace, regex and a live preview list. Batch renaming in Windows 11 covers it alongside the built-in F2 method.
- Names based on what the files contain: no command can tell that
scan0042.pdfis a lease agreement or thatIMG_4821.jpgshows a tram. File Renamer AI is a web app that reads each file you upload (images, PDFs, Office documents and more), suggests a descriptive name, lets you review the list, and gives you the renamed files as a ZIP.
Frequently Asked Questions
How do I rename multiple files at once in Command Prompt?
Open Command Prompt in the folder and use ren with wildcards for extension changes, for example ren *.txt *.md. For prefixes, suffixes or other changes, use a for loop such as for /f "delims=" %f in ('dir /b /a-d *.jpg') do ren "%f" "trip_%f", which renames each file individually.
What is the difference between ren and rename in cmd?
There is none. ren is the short form of rename and both take the same arguments: the current name, which may include a path, and the new name, which must not.
Why does ren *.jpg trip_*.jpg not add a prefix?
In the new name, ren treats literal characters as replacements for characters of the old name, position by position. The five characters of trip_ overwrite the first five characters of each name instead of being added in front. Use a for loop to add a prefix.
Can I undo a rename in Command Prompt?
No. Command Prompt has no undo, and Ctrl+Z in File Explorer does not reverse it. Preview the loop by putting echo in front of ren, check the output, and test on a copy of the folder before renaming important files.
Why do I need %%f instead of %f?
Loop variables use a single percent sign when you type a for command directly at the prompt and two percent signs inside a .bat or .cmd file. The same applies to modifiers, so %~nf at the prompt becomes %%~nf in a batch file.
Can ren move files to another folder?
No. The new name cannot contain a drive or path, and ren reports a syntax error if it does. Use the move command to move files, which can also rename them in the same step.
Free tools
Related Posts
How to Rename Files in Linux: mv, rename, Bash Loops and GUI Tools
How to rename a file in Linux with mv, rename directories, use both rename commands safely, and bulk rename files with bash loops, find and GUI file renamers.
WindowsHow to Rename Files with a Batch File (.bat Scripts You Can Reuse)
Write a .bat file to rename files on Windows: ren and for loops, numbering with zero padding, subfolders, dry runs, drag-and-drop folders and an undo log.
MacHow to Rename Files in Mac Terminal: mv, zsh Loops and zmv
Rename files from the Mac Terminal with mv, handle spaces and case-only changes, and bulk rename safely with zsh loops and zmv, with dry runs that preview first.
Ready to organize your files?
Try AI renaming right away. No sign-up needed until you download.
Try It Free