1

Here is the code which renames all the files in a folder to alphanumeric.

@echo off
setlocal disableDelayedExpansion
set "chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for /f "eol=: delims=" %%F in ('dir /b /a-d *.jpg') do call :renameFile "%%F"
exit /b

:renameFile
setlocal enableDelayedExpansion
:retry
set "name="
for /l %%N in (1 1 8) do (
  set /a I=!random!%%36
  for %%I in (!I!) do set "name=!name!!chars:~%%I,1!"
)
echo if exist !name!.jpg goto :retry
endlocal & ren %1 %name%.jpg

All you have to do is copy/paste this code and save it .bat file and place in any folders where you want to rename files.

Here is how it works:

example: if filename is lenovo-wallpaper.jpg then it will rename it to AF45ASLJ.JPG

What I want is filename+alphanumeric.jpg

example: if filename is lenovo-wallpaper.jpg then it should be lenovo-wallpaper-adfs45fad1.jpg

Jonno
  • 21,643
Dilip
  • 31

1 Answers1

1

Change the last two lines from

echo if exist !name!.jpg goto :retry
endlocal & ren %1 %name%.jpg

to

echo if exist %~n1+!name!.jpg goto :retry
endlocal & ren %1 %~n1+%name%.jpg

%~n1 will get the file name without an extension, and then just adding a + before the new variable name.

EDIT: Complete batch with capitalisation of the first letter, and capturing JPG, BMP, PNG and GIF files

@echo off
setlocal disableDelayedExpansion
set "chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for /f "eol=: delims=" %%F in ('dir /b /a-d *.jpg *.png *.bmp *.gif') do call :renameFile "%%F"
exit /b

:renameFile
setlocal enableDelayedExpansion
:retry
set "name="
for /l %%N in (1 1 8) do (
  set /a I=!random!%%36
  for %%I in (!I!) do set "name=!name!!chars:~%%I,1!"
)
echo if exist %~n1+!name!.jpg goto :retry
call :FirstUp result %~n1
endlocal & ren %1 %result%+%name%.*

exit /b

:FirstUp
setlocal EnableDelayedExpansion
set "temp=%~2"
set "helper=##AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ"
set "first=!helper:*%temp:~0,1%=!"
set "first=!first:~0,1!"
if "!first!"=="#" set "first=!temp:~0,1!"
set "temp=!first!!temp:~1!"
(
    endlocal
    set "result=%temp%"
    goto :eof
)
Jonno
  • 21,643