命令行(或批处理文件)重命名文件夹中的所有文件 (Windows)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7683048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:12:49  来源:igfitidea点击:

Command Line (or batch file) rename all files in a folder (Windows)

windowsbatch-filecommandcmdbatch-rename

提问by MartinaL

I need to rename all of the files in a folder with the following filename format;

我需要使用以下文件名格式重命名文件夹中的所有文件;

itemID-straight-bottle.png

itemID-straight-bottle.png

TO

itemID-bottle.png

itemID-bottle.png

How can i accomplish this with a cmd script or in the command line?

如何使用 cmd 脚本或在命令行中完成此操作?

example;

例子;

REDHI20806-straight-bottle.png TO REDHI20806-bottle.png

REDHI20806-straight-bottle.png 到 REDHI20806-bottle.png

I should have said, this is in Windows and i want to use either command line or a batch file to run this renaming on all files in a folder on a specific drive

我应该说,这是在 Windows 中,我想使用命令行或批处理文件在特定驱动器上的文件夹中的所有文件上运行此重命名

回答by tolitius

short answer

简答


On Windows, you can use PowerShell, that is installed by default on Windows 7, and can be downloaded and installed on previous versions. With PowerShell you can do the rename as:


Windows 上,您可以使用 Windows 7 上默认安装的 PowerShell,并且可以在以前的版本上下载和安装。使用 PowerShell,您可以将重命名为:

ls | foreach-object -process {ren $_ (%{$_ -replace "-straight",""})}

On Unix/Linux, nothing specific needs to be installed, and you can do the rename as:

Unix/Linux 上,不需要安装任何特定的东西,您可以将重命名为:

ls | awk '{print("mv "" ")}' | cut -f -3,5- -d '-' | sh



examples

例子

Windows Example

Windows 示例

Given

给定的

PS C:\rename-me> ls

    Directory: C:\rename-me

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         10/9/2011   1:35 PM          0 REDHI20806-straight-bottle.png
-a---         10/9/2011   1:35 PM          0 REDHI20807-straight-bottle.png
-a---         10/9/2011   1:35 PM          0 REDHI20808-straight-bottle.png
-a---         10/9/2011   1:35 PM          0 REDHI20809-straight-bottle.png
-a---         10/9/2011   1:35 PM          0 REDHI20810-straight-bottle.png

Doing It

正在做

PS C:\rename-me> ls | foreach-object -process {ren $_ (%{$_ -replace "-straight",""})}

Result

结果

PS C:\rename-me> ls

    Directory: C:\rename-me

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         10/9/2011   1:35 PM          0 REDHI20806-bottle.png
-a---         10/9/2011   1:35 PM          0 REDHI20807-bottle.png
-a---         10/9/2011   1:35 PM          0 REDHI20808-bottle.png
-a---         10/9/2011   1:35 PM          0 REDHI20809-bottle.png
-a---         10/9/2011   1:35 PM          0 REDHI20810-bottle.png



Unix/Linux Example

Unix/Linux 示例

Given

给定的

$ ls -l
total 0
-rw-r--r--  1 user  staff  0 Oct  7 00:54 REDHI20806-straight-bottle.png
-rw-r--r--  1 user  staff  0 Oct  7 00:54 REDHI20807-straight-bottle.png
-rw-r--r--  1 user  staff  0 Oct  7 00:54 REDHI20808-straight-bottle.png
-rw-r--r--  1 user  staff  0 Oct  7 00:54 REDHI20809-straight-bottle.png
-rw-r--r--  1 user  staff  0 Oct  7 00:54 REDHI20810-straight-bottle.png

Doing It

正在做

$ ls | awk '{print("mv "" ")}' | cut -f -3,5- -d '-' | sh

Result

结果

$ ls -l
total 0
-rw-r--r--  1 user  staff  0 Oct  7 00:54 REDHI20806-bottle.png
-rw-r--r--  1 user  staff  0 Oct  7 00:54 REDHI20807-bottle.png
-rw-r--r--  1 user  staff  0 Oct  7 00:54 REDHI20808-bottle.png
-rw-r--r--  1 user  staff  0 Oct  7 00:54 REDHI20809-bottle.png
-rw-r--r--  1 user  staff  0 Oct  7 00:54 REDHI20810-bottle.png

回答by wimh

if only the last part of the filename (straight-bottle.png) needs to change (to bottle.png) you can simply do this:

如果只有文件名 ( straight-bottle.png)的最后一部分需要更改 ( to bottle.png) 您可以简单地执行以下操作:

REN ??????????-straight-bottle.png ??????????-bottle.png

(I did not test this though)

(虽然我没有测试这个)