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

发布于 2024-12-08 18:03:01 字数 279 浏览 1 评论 0原文

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

itemID-straight-bottle.png

TO

itemID-bottle.png

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

例子;

REDHI20806-straight-bottle.png 至 REDHI20806-bottle.png

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

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

itemID-straight-bottle.png

TO

itemID-bottle.png

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

example;

REDHI20806-straight-bottle.png TO 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谁把谁当真 2024-12-15 18:03:01

简短回答

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

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

Unix/Linux 上,无需安装任何特定内容,您可以将重命名为:

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

示例

Windows 示例

给定

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

执行

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

结果

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 示例

给定

$ 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

执行

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

结果

$ 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

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:

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

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

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

examples

Windows Example

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

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 "$1" "$1)}' | 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
水溶 2024-12-15 18:03:01

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

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

我没有测试这个尽管)

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

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

(I did not test this though)

榆西 2024-12-15 18:03:01

我最终选择了 wimh 的答案 的修改版本,但不是“?????????” ,我使用了“*”,我认为它可以更好地处理不同长度的名称。

ren *-straight-bottle.png *-bottle.png

I ended up going with a modified version of wimh's answer but instead of "??????????", I used "*" which I think better handles names of different lengths.

ren *-straight-bottle.png *-bottle.png
牵你的手,一向走下去 2024-12-15 18:03:01

就我个人而言,我会使用专门为批量重命名而设计的工具,例如 renamer

此命令从当前目录中的所有文件中删除 -straight 文本:

$ renamer --find=-straight --dry-run *

Dry run

✔︎ REDHI20806-straight-bottle.png → REDHI20806-bottle.png
✔︎ GRNHI30030-straight-bottle.png → GRNHI30030-bottle.png

Rename complete: 2 of 2 files renamed.

Personally, I would use a tool designed specifically for bulk renaming, e.g. renamer.

This command strips out the -straight text from all files in the current directory:

$ renamer --find=-straight --dry-run *

Dry run

✔︎ REDHI20806-straight-bottle.png → REDHI20806-bottle.png
✔︎ GRNHI30030-straight-bottle.png → GRNHI30030-bottle.png

Rename complete: 2 of 2 files renamed.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文