How to Rename Files with a Batch File (.bat Scripts You Can Reuse)
To rename files with a batch file, put ren commands in a plain text file saved with a .bat extension and run it. A minimal script that adds a prefix to every JPG in its own folder looks like this:
@echo off
cd /d "%~dp0"
for /f "delims=" %%f in ('dir /b /a-d *.jpg') do ren "%%f" "trip_%%f"
pauseSave it as rename.bat in the folder with the photos and double-click it. The rest of this guide explains each line, then gives reusable scripts for numbering, subfolders, drag-and-drop, dry runs and undo.
If you only need a one-off rename typed at the prompt, renaming multiple files in Command Prompt covers ren wildcards and one-line loops in detail. This guide is about saved scripts you can run again.
Create the .bat File
- Open Notepad.
- Paste or type your script.
- Choose File > Save as.
- Set Save as type to All files.
- Name it something like
rename-photos.batand click Save.
Step 4 matters. If Notepad saves it as rename-photos.bat.txt, double-clicking opens it in Notepad instead of running it. Turn on file name extensions in File Explorer (View > Show > File name extensions) so you can see the real extension.
To edit the script later, right-click it and choose Edit (on Windows 11 it may be under Show more options), or open it from Notepad. Double-clicking runs it.
.cmd works the same as .bat for everything in this guide.
The Lines Every Rename Script Should Start With
@echo off
setlocal
cd /d "%~dp0"@echo offstops cmd from printing every command before running it.setlocalkeeps any variables the script sets from leaking into the Command Prompt session it was run from.cd /d "%~dp0"switches to the folder the .bat file is in.%~dp0is the drive and path of the script itself. Without this line, the script runs in whatever the current folder happens to be: usually the script's folder when you double-click it, butC:\Windows\System32if you choose Run as administrator, and the Command Prompt's current folder if you run it from there. A rename script that runs in the wrong folder is the classic batch file accident.
End scripts with pause when you double-click them, so the window stays open long enough to read any errors.
ren in a Batch File
ren (or rename, the same command) works exactly as it does at the prompt:
ren "old name.docx" "new name.docx"- Always quote names; spaces break unquoted names.
- The new name is a name only, with no folder path.
renwon't overwrite an existing file. You get "A duplicate file name exists, or the file cannot be found" and the script carries on with the next command.
A script can just be a list of these lines. That is handy when you have the old and new names in a spreadsheet: build the lines with a formula and paste them in. The Excel renaming guide shows the formula, and bulk renaming files from a list covers CSV-driven alternatives.
for Loops in a Batch File: %%f, Not %f
To rename many files, loop over them. Inside a .bat file the loop variable takes two percent signs: %%f. At the Command Prompt it takes one. Copying a prompt one-liner into a script without doubling them is the most common reason a rename .bat does nothing.
Modifiers give you parts of the name:
%%~nf: name without extension%%~xf: extension, including the dot%%~nxf: name and extension%%~ff: full path
Why the script uses for /f with dir
You will often see this simpler loop:
for %%f in (*.jpg) do ren "%%f" "trip_%%f"It reads the folder while it renames, and a renamed file still matches *.jpg, so the loop can meet it again and rename it twice: trip_trip_beach.jpg. Whether it happens depends on the file names and the drive, which makes it worse, not better. The fix is to take a complete list first:
for /f "delims=" %%f in ('dir /b /a-d *.jpg') do ren "%%f" "trip_%%f"dir /b gives bare names, /a-d excludes folders, and "delims=" keeps names with spaces whole. Use this form for any rename where the new name still matches the pattern. Changing extensions (*.txt to .md) is safe with the simple loop because the new names no longer match.
Do a Dry Run First
Batch renames have no undo. Make every script able to preview itself:
@echo off
setlocal
cd /d "%~dp0"
set "DO=echo"
for /f "delims=" %%f in ('dir /b /a-d *.jpg') do %DO% ren "%%f" "trip_%%f"
pauseWith set "DO=echo", the script prints each ren command instead of running it. Check the list, then change that line to set "DO=" and run it again to rename for real. Working on a copy of the folder the first time is still a good idea.
Number Files with Zero Padding
This script renames every JPG to photo_001.jpg, photo_002.jpg and so on, in name order:
@echo off
setlocal disabledelayedexpansion
cd /d "%~dp0"
set "DO=echo"
set n=0
for /f "delims=" %%f in ('dir /b /a-d /on *.jpg') do (
set "old=%%f"
set "ext=%%~xf"
set /a n+=1
setlocal enabledelayedexpansion
set "num=000!n!"
%DO% ren "!old!" "photo_!num:~-3!!ext!"
endlocal
)
pauseHow it works:
- A counter that changes inside a loop has to be read with delayed expansion,
!n!.%n%would be read once, before the loop runs, and every file would get the same number. set "num=000!n!"followed by!num:~-3!keeps the last three characters, which pads 7 to007. Use0000and~-4for four digits.- Delayed expansion is switched on only after the file name has been copied into
old. If it were on for the whole script, any!in a file name would be stripped and those files would fail to rename. Toggling it inside the loop avoids that. /onnumbers in name order. Use/odfor date order.
If photo_001.jpg already exists from an earlier run, that rename fails with the duplicate-name error. Run it on files that don't already use the target pattern. For other numbering approaches, see renaming files in sequence.
Replace Text in Every File Name
@echo off
setlocal disabledelayedexpansion
cd /d "%~dp0"
set "DO=echo"
for /f "delims=" %%f in ('dir /b /a-d') do (
set "old=%%f"
setlocal enabledelayedexpansion
set "new=!old: =_!"
if /i not "!old!"=="%~nx0" if not "!new!"=="!old!" %DO% ren "!old!" "!new!"
endlocal
)
pause!old: =_! replaces every space with an underscore. Change it to !old:draft=final! to swap a word, or !old:copy=! to remove one; the match is not case-sensitive. The if /i not "!old!"=="%~nx0" test stops the script renaming itself, which matters whenever the loop covers every file in its own folder.
Include Subfolders
for /r walks the current folder and everything below it. These lines assume the set "DO=echo" dry-run line from earlier in the script. Changing extensions recursively:
for /r %%f in (*.txt) do %DO% ren "%%f" "%%~nf.md"For anything where new names still match the pattern, list everything first with dir /s /b:
for /f "delims=" %%f in ('dir /s /b /a-d *.jpg') do %DO% ren "%%f" "trip_%%~nxf"In both, %%f is a full path, which ren accepts as the old name, while the new name is built from the name part only. Always dry-run recursive scripts; a mistake here touches every folder underneath.
Drag and Drop a Folder onto the .bat
A script that works on whatever folder you drop on it is more reusable than one that must sit inside the folder. When you drag a folder onto a .bat file in File Explorer, its path arrives as the first argument, %1. %~1 is the same path with surrounding quotes removed.
@echo off
setlocal
if "%~1"=="" (
echo Drag a folder onto this file.
pause
exit /b 1
)
if not exist "%~1\" (
echo That is not a folder.
pause
exit /b 1
)
pushd "%~1"
set "DO=echo"
for /f "delims=" %%f in ('dir /b /a-d *.jpg') do %DO% ren "%%f" "trip_%%f"
popd
pause- The first check handles double-clicking the script without dropping anything.
if not exist "%~1\"with a trailing backslash is true only when the path is not a folder.pushdchanges to the dropped folder (on any drive) andpopdreturns afterwards.
To handle dropped files instead of a folder, loop over all arguments with %*:
for %%a in (%*) do %DO% ren "%%~a" "trip_%%~nxa"Windows quotes dropped paths that contain spaces, but not ones with other special characters such as &, which can break the line, and very large selections hit the command-line length limit. For big drops, use the folder version.
Keep an Undo Log
Batch files can write the reverse of every rename as they go:
for /f "delims=" %%f in ('dir /b /a-d *.jpg') do (
ren "%%f" "trip_%%f" && >>undo-rename.bat echo ren "trip_%%f" "%%f"
)&& writes the undo line only if the rename succeeded. If the result is wrong, run undo-rename.bat from the same folder. Delete it once you're happy, so it can't be run by accident later.
Common Problems
- The window flashes and closes: add
pauseat the end, or run the script from a Command Prompt window to read the error. - Nothing happens, or an "unexpected at this time" error: the loop uses
%finstead of%%f. - Files were renamed twice: switch from
for %%f in (...)tofor /f "delims=" %%f in ('dir /b /a-d ...'). - The wrong folder was renamed: add
cd /d "%~dp0", or use the drag-and-drop version. - Accented characters come out garbled: Notepad saves as UTF-8, while cmd reads the script using the console code page. Adding
chcp 65001 >nulafter@echo offusually fixes it. - Files with ! in the name fail: use the toggled delayed expansion pattern from the numbering script.
- "Access is denied" or "The process cannot access the file": the file is open in another program or you lack permission to that folder. Close the program and try again.
When a Batch File Is the Wrong Tool
Batch files are good for prefixes, suffixes, numbering, extension changes and simple replacements you run repeatedly. They get awkward for regex, case changes and anything that needs a preview grid. PowerShell handles regular expressions and has -WhatIf built in, and PowerToys PowerRename gives a visual preview; batch renaming in Windows 11 compares them. If you'd rather not write scripts at all, you can rename files online for free, in your browser.
No script can name scan0042.pdf after the lease agreement it contains or IMG_4821.jpg after the tram in the picture. For that, File Renamer AI reads each uploaded file (images, PDFs, Office documents and more), suggests a descriptive name, and lets you review the list before downloading the renamed files as a ZIP.
Frequently Asked Questions
How do I make a batch file to rename files?
Open Notepad, write ren commands or a for loop, choose File > Save as, set Save as type to All files and save it with a .bat extension in the folder with your files. Start it with cd /d "%~dp0" so it works in its own folder, then double-click it.
Why do I need %%f instead of %f in a batch file?
In a .bat or .cmd file, cmd treats a single percent sign as the start of a variable, so for loop variables must be written with two: %%f. At an interactive Command Prompt you use one.
How do I rename files in a batch file without renaming some twice?
Use for /f "delims=" %%f in ('dir /b /a-d *.jpg') instead of for %%f in (*.jpg). The dir version builds the full list of names before renaming starts, so renamed files are not picked up again.
How can I test a rename batch file before running it?
Put echo in front of ren, or set a variable such as DO=echo and write %DO% ren in the loop. The script prints each command instead of running it. When the list looks right, clear the variable and run again.
Can a batch file rename files in subfolders?
Yes. for /r %%f in (*.txt) loops through the current folder and all subfolders, and for /f with dir /s /b does the same with a complete list taken first. Dry-run these first, because a mistake affects every folder below.
How do I rename a .bat file itself?
Rename it like any file: select it in File Explorer, press F2, type the new name and keep the .bat extension. To change a .txt script into a batch file, turn on file name extensions and change .txt to .bat.
Related Posts
How to Rename Multiple Files with Command Prompt (ren, for Loops and Batch Files)
Rename multiple files in Command Prompt with ren and for loops: change extensions, add prefixes, number files, include subfolders and preview first.
TutorialHow to Rename a File on Windows, Mac, Linux, iPhone and Android
How to rename a file on Windows, Mac, Linux, iPhone and Android: the rename shortcut for each, renaming folders and Word docs, and fixes for common errors.
TutorialHow 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.
Ready to organize your files?
Try AI renaming right away. No sign-up needed until you download.
Try It Free