Windows REN Command: Rename Files and Change Extensions From CMD

2026-12-02来源 dev.toEnglish

You need report.txt to become report.log, or a whole folder of .jpeg files to end in .jpg, and you don't want to click through File Explorer one file at a time. REN does that from CMD in one line. It's short, but it has a few rules that trip people up: it only takes a new name (never a path), it refuses to overwrite, and its wildcards don't work the way most people guess.

If you're new to moving around folders in CMD, map the folder first with the TREE command and create practice folders with MKDIR, then come back and rename things.

The syntax

From the ren page on Microsoft Learn:

ren [<drive>:][<path>]<filename1> <filename2> 
  • filename1 is the file (or set of files) you want to rename. It can include a drive, a path, and the wildcards * and ?.
  • filename2 is the new name only. It can also contain wildcards.
  • REN has no other switches besides /? for help.

RENAME is the same command with a longer name (rename on Microsoft Learn), so ren and rename are interchangeable.

Rename one file

ren report.txt report.log 

That renames report.txt in the current folder. On success CMD prints nothing and just returns to the prompt, so check with dir:

dir /b report.* 

You can point at a file in another folder by putting the path on the first argument only:

ren C:\Reports\report.txt report-2026.txt 

The file stays in C:\Reports. Only its name changes.

Names with spaces need quotes

Without quotes, CMD splits monthly report.txt into two arguments and gives up with a syntax error. Quote any name that contains a space:

ren "monthly report.txt" "monthly report - final.txt" 

Quoting every name is a good habit even when there are no spaces, especially in batch files where names come from variables.

Trap 1: the new name can't be a path

This is the most common REN mistake:

ren C:\Reports\report.txt C:\Reports\report.log 
The syntax of the command is incorrect. 

Microsoft Learn is explicit: you can't specify a new drive or path for the new name. The second argument must be just a file name. This is the classic Stack Overflow question about this exact error, and the fix is to drop the path from the second argument:

ren C:\Reports\report.txt report.log 

Trap 2: REN won't move files to another folder

Because the new name can't contain a path, REN also can't move a file into a different folder or onto another drive. If you want to rename and relocate in one step, that's a job for MOVE:

move C:\Reports\report.txt D:\Archive\report-2026.txt 

MOVE has its own surprises (a missing target folder silently becomes a file name), which I covered in Windows MOVE Command: Move Files Into a Folder Without Renaming Them by Accident.

Trap 3: "A duplicate file name exists, or the file cannot be found"

REN never overwrites. If report.log already exists, this fails:

ren report.txt report.log 
A duplicate file name exists, or the file cannot be found. 

Microsoft Learn shows the shorter wording Duplicate file name or file not found. Computer Hope's list of CMD errors lists both forms with the same meaning. Either way, the message covers two different problems, so check both:

dir /b report.txt dir /b report.log 
  • If report.txt isn't listed, the source name is wrong (typo, wrong folder, or a hidden extension like report.txt.txt).
  • If report.log is listed, the new name is already taken. Pick a different name, or delete or rename the old file first.

Rename a folder

REN works on folders too:

ren Chap10 Part10 ren "Old Projects" "Archived Projects" 

The catch: wildcards don't work for folders. According to SS64's REN reference, a wildcard rename that matches files and folders renames the files and ignores the folders, and one that only matches folders throws a syntax error. Rename folders one at a time, by full name.

Change extensions in bulk with wildcards

This is where REN earns its place. Microsoft Learn's own example changes every .txt file in the current folder to .doc:

ren *.txt *.doc 

The same pattern fixes a camera folder full of .jpeg files:

ren *.jpeg *.jpg 

photo1.jpeg becomes photo1.jpg, beach.jpeg becomes beach.jpg, and so on. Before you run a bulk rename, preview exactly which files the source pattern matches:

dir /b *.jpeg 

REN only works in the current folder (or the one folder in the path you give it). It doesn't recurse into subfolders.

How * and ? really map in the new name

Microsoft Learn's rule is one line: characters represented by wildcards in filename2 are identical to the corresponding characters in filename1. That sounds simple, but "corresponding" is doing a lot of work. The most detailed write-up I know is dbenham's tested rules for how RENAME interprets wildcards on Super User, which SS64 also reproduces. The rules below come from that write-up. CMD isn't something I can run on the box where I wrote this, so treat the examples as "what the documented rules say" and test on copies first.

*. followed by an extension changes only the last extension. The * copies everything up to the last dot:

ren * *.txt a -> a.txt b.dat -> b.txt c.x.y -> c.x.txt 

So ren *.txt *.log turns notes.v2.txt into notes.v2.log, and ren *.jpeg *.jpg turns holiday.2026.jpeg into holiday.2026.jpg. The middle dots survive.

Literal characters in the new name replace characters, they don't insert them. This is the big one. People expect ren *.txt x_*.txt to add a prefix. It doesn't. Each literal character overwrites one character of the original name, left to right:

ren *.txt x_*.txt report.txt -> x_port.txt 

x replaced r, _ replaced e, and then * copied the rest. Computer Hope's REN page gives the same warning with its own example: hope.txt becomes 1_pe.txt.

? copies one character from the original name (and never copies a dot). You can use it to keep the start of a name and change a character in a fixed position:

ren * A?Z* 12.txt -> A2Z.txt 1234.txt -> A2Z4.txt 

The source pattern can also match short 8.3 names. On volumes that still generate short names, filename1 is compared against both the long name and the short name, so a pattern can occasionally match a file you didn't expect. Another reason to run dir /b with the same pattern first.

Add a prefix the right way

Since REN wildcards can't insert text at the start of a name, loop over the files and build each new name yourself. At the command prompt:

for /f "delims=" %f in ('dir /b /a-d *.txt') do ren "%f" "x_%f" 

In a .bat file, double the percent signs:

for /f "delims=" %%f in ('dir /b /a-d *.txt') do ren "%%f" "x_%%f" 

dir /b /a-d produces the list of file names (no folders) before the loop starts, delims= keeps names with spaces in one piece, and the quotes protect those spaces when REN runs. To test it first, put echo in front of ren and read the commands it would run.

The PowerShell equivalent

If you're in PowerShell, Rename-Item does the same job, and with -NewName { ... } you can build each new name with real string logic (prefixes, counters, replacing text in the middle). I covered that in PowerShell Rename-Item: Bulk Rename Hundreds of Files with One Line.

Quick reference

Goal Command
Rename one file ren report.txt report.log
Name with spaces ren "old name.txt" "new name.txt"
File in another folder ren C:\Reports\a.txt b.txt (path on the first argument only)
Rename a folder ren Chap10 Part10 (no wildcards)
Change all extensions ren *.txt *.log
Rename and move use move, not ren
Add a prefix for /f loop, not ren *.txt x_*.txt

Related CMD tips:

Official reference: ren on Microsoft Learn.

Practice it without risking real files

Bulk renames are exactly the kind of command you want to try on throwaway files first. You can practice CMD rename commands in a free browser terminal with CMD Master's interactive lessons. Nothing touches your real files, and there are free hands-on lessons for CMD, PowerShell, and Bash once you've got REN down.

Quick check: a folder contains server.log and app.log. You run ren *.log old_*.log. Using the "literal characters replace, they don't insert" rule, what are the two new file names? Answer in the comments.

原文:https://www.a8q9.cn/en/article/windows-ren-command-rename-files-and-change-extensions-from-cmd-3c9a36.html

相关文章