大网数据广告活动
Windows排障命令

rename

· 52CE 我爱测

Learn how to use the rename command to rename files or directories from the command line in Windows Server, including wildcard examples for batch renaming.

Renames one or more files or directories. Use wildcard characters (* and ?) to rename multiple files in a single command.

Note: This command is the same as the ren command.

Syntax

rename [<drive>:][<path>]<filename1> <filename2>

Parameters

Parameter Description
[<drive>:][<path>]<filename1> Specifies the location and name of the file or set of files you want to rename. Filename1 can include wildcard characters (* and ?).
<filename2> Specifies the new name for the file. Use wildcard characters to specify new names for multiple files.
/? Shows help at the command prompt.
Remarks
  • You can't specify a new drive or path when renaming files. You also can't use this command to rename files across drives or to move files to a different directory.

  • Wildcard characters in filename2 represent the corresponding characters in filename1.

  • Filename2 must be a unique file name. If filename2 matches an existing file name, the following message appears: Duplicate file name or file not found.

Examples

To change all the .txt file name extensions in the current directory to .doc extensions, type:

rename *.txt *.doc

When you use wildcards in filename2, the command replaces each wildcard with the characters in the same position in the original name. A ? keeps the single character in that position, and a * keeps all remaining characters from that position to the end of the name or extension. Because the match is positional, the results can be surprising.

To replace a fixed-length prefix in a set of file names while keeping the rest of each name, type:

rename img*.jpg pic*.jpg

Because img and pic are both three characters, each * starts at the same position, so only the prefix changes:

Original file name New file name
img001.jpg pic001.jpg
img_final.jpg pic_final.jpg

Note: The replacement prefix must be the same length as the text it replaces. For example, rename img*.jpg photo*.jpg renames img001.jpg to photo1.jpg, because photo is five characters and overwrites the first five characters (img00) of the original name.

To change a single character at a known position and keep the rest of the name, type:

rename ??-*.log ??_*.log

This command matches names whose third character is a hyphen and replaces only that hyphen with an underscore:

Original file name New file name
ab-server.log ab_server.log
db-audit.log db_audit.log

To append an extension without removing the current one, type:

rename *.log *.log.bak

The * keeps the original name and extension, and the command appends .bak to the end:

Original file name New file name
server.log server.log.bak
app.debug.log app.debug.log.bak

To change the name of a directory from Chap10 to Part10, type:

rename chap10 part10

Related links

Source: Microsoft Learn / Windows Server documentation by Microsoft and contributors (upstream author metadata: robinharwood). Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Server documentation
适用范围
Windows Server · Source date 10/16/2017
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。