HOWTO · Batch

How to Rename a File in a Batch Script

Learn how to rename one file or a group of files in a Windows batch script with ren, rename, wildcards, paths, variables, and for loops.

On this page

Use the ren (or rename) command in a Windows batch file to change a file name without moving the file. The command works in cmd.exe, and a .bat or .cmd file can apply it repeatedly to matching files. The examples below use the current directory unless a source path is included.

Basic Rename Command in Batch Script

The syntax is:

ren <old-name> <new-name>

rename is an equivalent, longer spelling of ren. Quote names that contain spaces:

@echo off
ren "old report.txt" "new report.txt"

On success, cmd.exe normally prints no message. Verify the result with dir or by checking that the new name exists. The destination is a new name, not a second path: ren cannot move a file to another directory or drive. Use move when the operation must also change directories.

Rename Multiple Files with Wildcards

Wildcards let one command match a set of source names. This command changes the extension of every .txt file in the current directory while retaining each base name:

@echo off
ren "*.txt" "*.bak"

For example, notes.txt becomes notes.bak and report.txt becomes report.bak. The * in the destination corresponds to the source base name; it does not give every file the literal name *.bak. The ? wildcard matches one character position and can narrow a pattern, for example ren "log?.txt" "archived-?.txt".

Rename a File in a Specific Directory

The source can include a drive and directory:

@echo off
ren "C:\Work\incoming\report.txt" "report-old.txt"

The renamed file remains in C:\Work\incoming. Do not write a directory in the second argument, such as ren "C:\Work\incoming\report.txt" "C:\Work\archive\report.txt"; that is a move and is outside ren’s syntax. Run the batch file with permissions that allow changes in the source directory.

Use Variables and a for Loop

Variables are useful when the old or new name is assembled by another command or entered by a user. Quote the variable assignment so the value does not acquire stray spaces:

@echo off
set "oldname=old report.txt"
set "newname=reviewed report.txt"
ren "%oldname%" "%newname%"

To add a prefix to every .txt file, use a for loop. %%F is the batch-file form of the loop variable, and %%~nxF expands to the original name and extension:

@echo off
for %%F in (*.txt) do ren "%%F" "new_%%~nxF"

Test this in a copy of the directory first. A destination that already exists can cause a rename to fail, and a source pattern that matches no files does not rename anything. A loop can also encounter a collision partway through, so inspect the directory after the command and keep backups when names matter.

Common Questions

Are ren and rename different commands?

No. In cmd.exe, rename is the long form of ren.

Why did a rename fail?

Check that the source exists, the destination is not already in use, the quoting is correct, and the batch file has access to the directory. A missing source produces an error rather than creating a new file. A destination path is also invalid because ren renames within the source directory.

Can I rename a directory?

Yes, ren can rename a directory, but the destination still cannot specify a different path. Use move for relocation.