Batch Rename Files with a .bat File (ren and for Loops)
Updated
A batch file is a plain text file with the extension .bat that Windows runs line by line in the Command Prompt. It is a good fit when the same rename job comes back regularly, for example every month's scanned invoices. This guide explains the ren command, for loops and the pitfalls that make batch renaming go wrong.
Create and run a batch file
- Open Notepad and paste one of the scripts below.
- Choose File › Save as, set Save as type to All files, and save it as
rename.batin the folder with the files. - Double-click
rename.bat, or open a Command Prompt in the folder and typerename.bat.
The scripts work in the folder they are started from. Test every script on a copy of the folder first: renames done by a batch file cannot be undone with Ctrl+Z.
The ren command
ren (short for rename) changes the name of a file in place. It cannot move files to another folder, and it fails with an error if a file with the new name already exists.
ren report.txt report-2026.txt
ren "monthly report.docx" monthly-report.docx
ren *.jpeg *.jpgNames with spaces need quotes. With wildcards, ren *.jpeg *.jpg changes the extension of all .jpeg files at once. Wildcards in the new name follow special rules, so for anything more than an extension change use a for loop.
for loops and name modifiers
A for loop runs a command once per file. Inside a batch file the loop variable is written with two percent signs (%%F); typed directly at the prompt it has one (%F). Modifiers split the name:
| Modifier | Meaning | For photo.jpeg |
|---|---|---|
%%F | full name | photo.jpeg |
%%~nF | name without extension | photo |
%%~xF | extension with dot | .jpeg |
%%~nxF | name and extension | photo.jpeg |
Add a prefix to every PDF
@echo off
for /f "delims=" %%F in ('dir /b /a-d *.pdf') do (
ren "%%F" "2026-11_%%F"
)Why for /f with dir /b instead of the simpler for %%F in (*.pdf)? The simple loop reads the folder while it renames, and a renamed file can show up in the loop a second time, ending as 2026-11_2026-11_invoice.pdf. dir /b /a-d lists all files first (bare names, no folders) and the loop then works through that fixed list. "delims=" keeps names with spaces in one piece.
Add a suffix before the extension
@echo off
for /f "delims=" %%F in ('dir /b /a-d *.jpg') do (
ren "%%F" "%%~nF_edited%%~xF"
)Number files with leading zeros
@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=00!n!"
ren "%%F" "holiday_!num:~-3!%%~xF"
)
endlocalEnableDelayedExpansionlets you use!n!to read the counter's current value inside the loop;%n%would be fixed when the loop starts.set "num=00!n!"adds two zeros in front, and!num:~-3!keeps the last three characters:1becomes001,12becomes012./onsorts the list by name. Use/odto number by date instead.- With delayed expansion turned on, file names that contain
!are not handled correctly. Rename such files by hand first.
Replace text in file names
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%F in ('dir /b /a-d *.*') do (
set "old=%%F"
set "new=!old: =_!"
if not "!new!"=="!old!" ren "%%F" "!new!"
)
endlocal!old: =_! replaces every space with an underscore. The general form is !variable:search=replace!; for example !old:draft=final! replaces the word draft. The if line skips files whose name does not change. Note that this script would also rename itself if its own name contained a space.
Dry run: see what would happen
Put echo in front of ren and add pause as the last line. The script then prints every rename command instead of running it, and the window stays open so you can read the list:
@echo off
for /f "delims=" %%F in ('dir /b /a-d *.jpg') do (
echo ren "%%F" "%%~nF_edited%%~xF"
)
pauseIf the output looks right, remove echo and run the script again.
Batch file, PowerShell or a browser tool?
Batch files run on every Windows version without setup, but their syntax gets awkward quickly. For rules based on dates, regular expressions or subfolders, PowerShell is easier to read; see the PowerShell section in bulk rename files in Windows. For a quick one-time job without writing code, add the files to the online bulk renamer on bulkrename.com, apply a pattern like holiday_[num] and download the renamed copies as a ZIP.