1

I want to rename thousands of images (several extensions; png, jpg, gif) in a certain folder with the folder name followed by a ascending number, but the problem is when images have multiple dots/periods in the filename, the name gets cut off after the first period, than the file gets a wrong extension. For example: Folder name: galaxies and nebulae Imageformates: *.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

The problem is when a file has multiple periods like

text.word.age.region.png

it will be changed to

galaxies_and_nebulae_01.wor

because it gets cut off right after the first period and gets a new extension from the following three letters because of the wildcards (???). And I have no clue how to avoid this. Please no PowerShell or renaming programms like FART.

0

Try to replace:

  • .??? to %%~xi == .extension

  • ren "%%i"... to ren "%%~fi"...

With this ??? == 3/max/lenght you prevent work with your 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

Read more about:

| improve this answer | |
0

You need to be careful when using delayed expansion. Expansion of FOR variables will be corrupted if the value contains ! and delayed expansion is enabled. This is important because ! is valid in file and folder names.

Also, you need to make sure that you don't already have file names that match your final file name format, else you might get rename collisions.

Here is a solution that avoids any need of delayed expansion, but the sequence numbers are not left padded with zeros to a constant width.

@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"
)

Here is a variation that zero pads all sequence numbers to a constant width. Delayed expansion is toggled on and off in a way that protects file/folder names with !.

@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
)

If you don't care about the sort order, then it can be really simple using my hybrid batch/JScript regular expression renaming utility JREN.BAT:

@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"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.