我想重命名某个文件夹中的数千张图像(多个扩展名:png、jpg、gif),文件夹名称后跟一个升序数字,但问题是当图像的文件名中有多个点/句点时,名称会在第一个句点后被截断,然后文件会得到错误的扩展名。例如:文件夹名称:galaxies and nebulae 图像格式:*.gif、*.jpg、*.png、*.tiff
@echo off
setlocal EnableDelayedExpansion
rem get folder name and show it
for %%A in (.) do (
echo %%~nA
set fona=%%~nA
)
rem replayce blanks with underline, set suffix, set new folder name
set "blank= "
set "undl=_"
set suffix=100
set nfona=!fona:%blank%=%undl%!
rem search images and rename
for /F "delims=" %%i in ('dir /B /oe /od *.png *.jpg *.tiff *.gif') do (
set /A suffix+=1
ren "%%i" "!nfona!_!suffix:~1!.???"
)
pause
问题是当一个文件有多个句点时,例如
text.word.age.region.png
它将更改为
galaxies_and_nebulae_01.wor
因为它会在第一个句点后立即被截断,并会因为通配符(???)而从后面三个字母获得新的扩展名。我不知道如何避免这种情况。请不要使用 PowerShell 或重命名程序(如 FART)。
答案1
尝试替换:
.???
到%%~xi
==.extension
ren "%%i"...
到ren "%%~fi"...
有了这个??? == 3/max/lenght
你妨碍了你的工作files.tiff
==.????
@echo off && setlocal EnableDelayedExpansion
for %%A in (.)do echo/.\%%~nA\ & set "fona=%%~nA"
for /f tokens^=* %%i in ('dir /b /oe /od *.png *.jpg *.tiff *.gif
')do set/a "suffix+=1+100+0" && call ren "%%~fi" "!fona: =_!_!suffix:~-2!%%~xi"
echo=^| total files renamed in folder: !suffix:~-2! && timeout -1 & endlocal & goto :EOF
阅读更多关于:
答案2
使用延迟扩展时需要小心。如果值包含!
并且启用了延迟扩展,则 FOR 变量的扩展将被破坏。这很重要,因为!
在文件和文件夹名称中有效。
此外,您需要确保您还没有与最终文件名格式匹配的文件名,否则可能会发生重命名冲突。
这是一个避免任何延迟扩展需要的解决方案,但序列号不会用零填充到恒定宽度。
@echo off
setlocal disableDelayedExpansion
:: Get folder name with spaces replaced by underscore
for %%F in (.) do set "fldr=%%~nF"
set "fldr=%fldr: =_%"
:: Define the list of extensions to process - the leading space is important
set "ext= png jpg tiff gif"
:: Rename any files that already match the intended name format to something else so there cannot be any collisions
:: This code simply appends underscore to the base name
for %%X in (%ext%) do 2>nul ren "%fldr%_????????????????????????????????????????????????????????????????????????????????????????????????????.%%X" "??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????_.*"
:: List the files sorted by extension, then last modified date, and prefix each name with a sequence number followed by a colon
for /f "delims=" %%F in (
'dir /b /oed %ext: = *.% ^| findstr /n "^"'
) do (
%= Parse out the sequence number and file name and rename the file =%
for /f "delims=: tokens=1,2" %%A in ("%%F") do ren "%%B" "%fldr%_%%A%%~xB"
)
这是一个变体,它将所有序列号用零填充到固定宽度。延迟扩展以保护文件/文件夹名称的方式打开和关闭!
。
@echo off
setlocal disableDelayedExpansion
:: Define the list of extensions to process - the leading space is important
set "ext= png jpg tiff gif"
:: Get folder name with spaces replaced by underscore
for %%F in (.) do set "fldr=%%~nF"
set "fldr=%fldr: =_%"
:: Rename any files that already match the intended name format to something else so there cannot be any collisions
:: This code simply appends underscore to the base name. Add additional ? as needed if you have really long file names.
for %%X in (%ext%) do 2>nul ren "%fldr%_????????????????.%%X" "??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????_.*"
:: Count the number of files so we know how many digits are needed
for /f %%N in ('dir /b /oed %ext: = *.% ^| find /c /v ""') do set "seq=%%N"
:: Convert each digit to 0, then prefix with 1
setlocal enableDelayedExpansion
for %%D in (1 2 3 4 5 6 7 8 9) do call set "seq=!seq:%%D=0!"
endlocal & set "seq=1%seq%"
:: Rename the files. Note that delayed expansion must be toggled in case name contains !
for /f "delims=" %%F in ('dir /b /oed %ext: = *.%') do (
set /a seq+=1
set "file=%%F"
set "ext=%%~xF"
setlocal enableDelayedExpansion
ren "!file!" "!fldr!_!seq:~1!!ext!"
endlocal
)
如果你不关心排序顺序,那么使用我的混合批处理/JScript 正则表达式重命名实用程序会非常简单杰任巴特:
@echo off
setlocal disableDelayedExpansion
:: Get folder name with spaces replaced by underscore
for %%F in (.) do set "fldr=%%~nF"
set "fldr=%fldr: =_%"
:: Temporarily rename existing PNG JPG TIFF and GIF to avoid possibility of rename collisions
call jren "\.(png|jpg|tiff|gif)$" "_$&"
:: Rename all PNG JPG TIFF and GIF to the folder name + a sequence number padded to 4 digits
call jren ".*\." "'%fldr%_'+$n+'.'" /j /npad 4 /fm "*.png|*.jpg|*.tiff|*.gif"