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

  • 1
    You have not asked a question, let alone a programming question. If you have a problem with your code, you should state exactly what is failing, and how, as well as what the expected behavior is. – dbenham Feb 9 '16 at 19:03
  • 1
    @dbenham Actually he has a "problem" with your code. It's been lifted from Batch file rename with random alphanumeric ;) – DavidPostill Feb 9 '16 at 20:27
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
)
  • A tiny refinement: if you use .* in the new name, the case of the extension will be maintained (ie ren %1 %~n1+%name%.*). – AFH Feb 9 '16 at 18:30
  • @AFH True, I didn't want to modify it much further (Only .jpg files are caught by the for loop anyway) :) – Jonno Feb 9 '16 at 18:31
  • I realised that, but the existing code renames Name.JPG to Name+8chars.jpg. The comment was directed as much to the questioner as to you. – AFH Feb 9 '16 at 18:37
  • @AFH Understood :) – Jonno Feb 9 '16 at 18:38
  • Can you fix it for all imag extensions like jpg, gif, png, bmp ? – Dilip Feb 10 '16 at 1:00

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.