重命名多个文件并重新编号文件

重命名多个文件并重新编号文件

我的文件夹中有很多文件,例如:

ConstantText1_variable_name_1_-_Constant_Text2_1233.png
ConstantText1_variable_name_1_-_Constant_Text2_1234.png
ConstantText1_variable_name_1_-_Constant_Text2_1235.png

ConstantText1_variable_name_2_-_Constant_Text2_1236.png
ConstantText1_variable_name_2_-_Constant_Text2_1237.png
ConstantText1_variable_name_2_-_Constant_Text2_1238.png
ConstantText1_variable_name_2_-_Constant_Text2_1239.png

ConstantText1_another_variable_name_3_-_Constant_Text2_1240.png
ConstantText1_another_variable_name_3_-_Constant_Text2_1241.png

我想重命名它们,使它们变成:

variable_name_1_01.png
variable_name_1_02.png
variable_name_1_03.png

variable_name_2_01.png
variable_name_2_02.png
variable_name_2_03.png
variable_name_2_04.png

another_variable_name_3_01.png
another_variable_name_3_02.png

我尝试了各种脚本来获取文件名并操作字符串,但都无法正常工作。
我一直在使用 BulkRenameUtility 并分别重命名每个“variable_name”,但我宁愿使用更高效、更简单的方法。谢谢。

答案1

在批处理中:

@echo off
cls
setlocal enabledelayedexpansion
set number=1
set prevstr=*
for /f %%x in ('dir /b *.png') do (
    call :sub %%x
)
endlocal
goto :eof

:sub
set tmpstr=%1
set tmpstr=%tmpstr:ConstantText1_=%
set tmpstr=%tmpstr:_-_Constant_Text2_=*%
for /f "tokens=1 delims=*" %%x in ("%tmpstr%") do (
    if not %prevstr%==%%x ( 
        set number=1
        set prevstr=%%x
    )
    echo ren "%1" "%%x_!number!.png"
    set /a number+=1
)
exit /b

有没有办法填充数字?例如)[01,02,...,09,10]而不是[1,2,...,9,10]?

当然。你看 - 我使用零填充到 3 位数字,如果需要,可以更改金额

@echo off
cls
setlocal enabledelayedexpansion
set number=1
set prevstr=*
for /f %%x in ('dir /b *.png') do (
    call :sub %%x
)
endlocal
goto :eof

:sub
set tmpstr=%1
set tmpstr=%tmpstr:ConstantText1_=%
set tmpstr=%tmpstr:_-_Constant_Text2_=*%
for /f "tokens=1 delims=*" %%x in ("%tmpstr%") do (
    if not %prevstr%==%%x ( 
        set number=1
        set prevstr=%%x
    )
:: zero padding to 3 digits, for another length alter next 2 lines accordingly
    set padded=000!number!
    echo ren "%1" "%%x_!padded:~-3!.png"
    set /a number+=1
)
exit /b

答案2

以下是依赖于两个混合批处理/JScript 实用程序脚本的解决方案:

  • 杰任巴特:正则表达式重命名实用程序
  • 排序命令:一种能够仅输出唯一值的排序实用程序

这两个实用程序都是纯脚本,可​​以在 XP 及更高版本的任何 Windows 机器上本地运行,无需任何第三方 exe 文件。

使用jren /?jsort /?获取有关每个实用程序可用的众多选项的帮助。

以下代码对我有用。根据需要更改 txt1、txt2 和 pad 的定义。

@echo off
setlocal disableDelayedExpansion

set "txt1=ConstantText1_"
set "txt2=_-_Constant_Text2_"
set "pad=2"

call :escape txt1
call :escape txt2
set "find=^.*$"

for /f "eol=: delims=" %%A in (
  'jren "^%txt1regex%(.*)%txt2regex%.*\.png$" "$1" /i /list /fm "%txt1%*%txt2%*.png" ^| jsort /i /u'
) do (
  set "nm=%%A"
  set "fm=%txt1%%%A%txt2%*.png"
  setlocal enableDelayedExpansion
  call jren "!find!" "'!nm!_'+$n+'.png'" /j /npad %pad% /fm !fm!
  endlocal
)
exit /b

:escape
setlocal enableDelayedExpansion
set "txt=!%~1!"
set "txt=!txt:.=\.!"
set "txt=!txt:{=\{!"
set "txt=!txt:[=\[!"
set "txt=!txt:^^=\^^!"
set "txt=!txt:$=\$!"
set "txt=!txt:+=\+!"
set "txt=!txt:(=\(!"
set "txt=!txt:)=\)!"
endlocal & set "%~1regex=%txt%"
exit /b

以下是示例输出,显示上述脚本与示例文件名配合使用。我将脚本命名为“test.bat”

C:\test\renameTest>dir
 Volume in drive C has no label.
 Volume Serial Number is 5ED1-638E

 Directory of C:\test\renameTest

02/27/2020  11:23 PM    <DIR>          .
02/27/2020  11:23 PM    <DIR>          ..
02/27/2020  11:21 PM                 0 ConstantText1_another_variable_name_3_-_Constant_Text2_1240.png
02/27/2020  11:21 PM                 0 ConstantText1_another_variable_name_3_-_Constant_Text2_1241.png
02/27/2020  11:21 PM                 0 ConstantText1_variable_name_1_-_Constant_Text2_1233.png
02/27/2020  11:21 PM                 0 ConstantText1_variable_name_1_-_Constant_Text2_1234.png
02/27/2020  11:21 PM                 0 ConstantText1_variable_name_1_-_Constant_Text2_1235.png
02/27/2020  11:21 PM                 0 ConstantText1_variable_name_2_-_Constant_Text2_1236.png
02/27/2020  11:21 PM                 0 ConstantText1_variable_name_2_-_Constant_Text2_1237.png
02/27/2020  11:21 PM                 0 ConstantText1_variable_name_2_-_Constant_Text2_1238.png
02/27/2020  11:21 PM                 0 ConstantText1_variable_name_2_-_Constant_Text2_1239.png
02/27/2020  11:21 PM               599 createFiles.bat
02/27/2020  11:23 PM               775 test.bat
              11 File(s)          1,374 bytes
               2 Dir(s)  537,716,002,816 bytes free

C:\test\renameTest>test
"C:\test\renameTest\ConstantText1_another_variable_name_3_-_Constant_Text2_1240.png"  -->  "another_variable_name_3_01.png"
"C:\test\renameTest\ConstantText1_another_variable_name_3_-_Constant_Text2_1241.png"  -->  "another_variable_name_3_02.png"
"C:\test\renameTest\ConstantText1_variable_name_1_-_Constant_Text2_1233.png"  -->  "variable_name_1_01.png"
"C:\test\renameTest\ConstantText1_variable_name_1_-_Constant_Text2_1234.png"  -->  "variable_name_1_02.png"
"C:\test\renameTest\ConstantText1_variable_name_1_-_Constant_Text2_1235.png"  -->  "variable_name_1_03.png"
"C:\test\renameTest\ConstantText1_variable_name_2_-_Constant_Text2_1236.png"  -->  "variable_name_2_01.png"
"C:\test\renameTest\ConstantText1_variable_name_2_-_Constant_Text2_1237.png"  -->  "variable_name_2_02.png"
"C:\test\renameTest\ConstantText1_variable_name_2_-_Constant_Text2_1238.png"  -->  "variable_name_2_03.png"
"C:\test\renameTest\ConstantText1_variable_name_2_-_Constant_Text2_1239.png"  -->  "variable_name_2_04.png"

C:\test\renameTest>dir
 Volume in drive C has no label.
 Volume Serial Number is 5ED1-638E

 Directory of C:\test\renameTest

02/27/2020  11:23 PM    <DIR>          .
02/27/2020  11:23 PM    <DIR>          ..
02/27/2020  11:21 PM                 0 another_variable_name_3_01.png
02/27/2020  11:21 PM                 0 another_variable_name_3_02.png
02/27/2020  11:21 PM               599 createFiles.bat
02/27/2020  11:23 PM               775 test.bat
02/27/2020  11:21 PM                 0 variable_name_1_01.png
02/27/2020  11:21 PM                 0 variable_name_1_02.png
02/27/2020  11:21 PM                 0 variable_name_1_03.png
02/27/2020  11:21 PM                 0 variable_name_2_01.png
02/27/2020  11:21 PM                 0 variable_name_2_02.png
02/27/2020  11:21 PM                 0 variable_name_2_03.png
02/27/2020  11:21 PM                 0 variable_name_2_04.png
              11 File(s)          1,374 bytes
               2 Dir(s)  537,715,937,280 bytes free

C:\test\renameTest>

答案3

当你准备好探索时电源外壳

$Folder = $pwd.path   ### Edit appropriately. This is current location.
$CT1 = 'ConstantText1_'
$CT2 = '_Constant_Text2_'
Get-ChildItem $Folder *.png -File | ForEach{
    $_.BaseName -replace $CT1 -replace $CT2 -match '^(\w+)\-(\d+)$' | out-null
    [PSCustomObject]@{
        'Path'     = $_.FullName
        'BaseName' = $matches[1]
        'Index'    = $matches[2]
    }
} | Group BaseName | ForEach {
    [ref]$i = 1
    $_.Group | Sort Index | 
        Rename-Item -NewName { '{0}{1:00}.png' -f $_.BaseName, $i.Value++ }
}

相关内容