批处理:如何批量重命名文件(接近)?

发布于 2024-12-28 13:55:20 字数 540 浏览 3 评论 0原文

我见过人们在 Perl 中这样做,但我想知道是否有办法通过批处理来做到这一点?它内置于 Windows 中,因此我认为了解如何使用批处理脚本来执行此操作会更有用。它不需要在计算机上安装任何东西。

输入名称示例:myFile_55 输出模式示例:将 myFile 更改为 picture 并将数字减少 13。 示例输出:picture_42

你会如何处理这个问题?我知道一个用于重命名的批处理命令:

ren myFile_55 picture_42

因此,如果我有一个名为 renamer.bat 的文件,我可以添加以下内容:

for /r %%x in (%1) do ren "%%x" %2代码>.

然后我可以输入以下命令:

renamer.bat myfile* picture*

但我不知道如何减少数量。

I've seen people do this in Perl, but I'm wondering if there's a way to do it via batch? It's built into Windows, so I think it would be more useful to know how to do this with a batch script. It doesn't require installing anything onto a computer.

An example input name: myFile_55
An example output pattern: change myFile to picture and reduce the number by 13.
An example output: picture_42.

How would you approach this? I know a batch command to rename:

ren myFile_55 picture_42.

So, if I have a file named renamer.bat, I can add the following:

for /r %%x in (%1) do ren "%%x" %2.

Then I can type this command:

renamer.bat myfile* picture*.

I don't know how to reduce the numbers, though.

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

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

发布评论

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

评论(1

爱,才寂寞 2025-01-04 13:55:20

您可以将原始文件名放入 for 循环中,并提取名称和数字,对数字进行数学运算,然后使用新的名称和数字将其重新插入。只要文件名格式为 name_number,您就可以使用以下命令:

REM Allow for numbers to be iterated within the for-loop i.e. the i - z
SETLOCAL ENABLEDELAYEDEXPANSION
SET i=0
SET z=13
SET newName=picture
SET workDir=C:\Path\To\Files

REM Given that filenames are in the format of 'Name_number', we're going to extract 'number
REM and set it to the i variables, then subtract it by 13, then rename the original file
REM to what the newName_x which if the filename was oldName_23 it would now be newName_10
FOR /r %%X in (%1) do (
  FOR /F "tokens=1-2 delims=_" %%A IN ("%%X") DO (
    SET i=%%B
    SET /A x=!i! - %z%

    REM ~dpX refers to the drive and path of the file
    REN "%%~dpX\%%A_%%B" "%newName%_!x!"
  )
)

编辑:编辑 REN 命令以包含原始文件的驱动器和路径。从小写 x 更改为大写 X,以免混淆 %%~dpX

You can probably put the original file name through a for loop, and extract the name and numbers, do the math on the numbers, then plug it back in with the new name and number. As long as the filename format is name_number you can use this:

REM Allow for numbers to be iterated within the for-loop i.e. the i - z
SETLOCAL ENABLEDELAYEDEXPANSION
SET i=0
SET z=13
SET newName=picture
SET workDir=C:\Path\To\Files

REM Given that filenames are in the format of 'Name_number', we're going to extract 'number
REM and set it to the i variables, then subtract it by 13, then rename the original file
REM to what the newName_x which if the filename was oldName_23 it would now be newName_10
FOR /r %%X in (%1) do (
  FOR /F "tokens=1-2 delims=_" %%A IN ("%%X") DO (
    SET i=%%B
    SET /A x=!i! - %z%

    REM ~dpX refers to the drive and path of the file
    REN "%%~dpX\%%A_%%B" "%newName%_!x!"
  )
)

EDIT: Edited the REN command to include the drive and path of the original file. Changed from lower case x to upper case X as to not confuse %%~dpX.

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