批:如何批量重命名文件(接近)?
-
12-11-2019 - |
题
我见过人们在 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*
.
但我不知道如何减少数量。
没有正确的解决方案
其他提示
您可以将原始文件名放入 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
.
不隶属于 StackOverflow