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,049
  • 4
  • 61
  • 70
Dilip
  • 31
  • 1
  • 6
  • 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 09 '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](http://superuser.com/a/776801) ;) – DavidPostill Feb 09 '16 at 20:27

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,049
  • 4
  • 61
  • 70
  • 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 09 '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 09 '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 09 '16 at 18:37
  • @AFH Understood :) – Jonno Feb 09 '16 at 18:38
  • Can you fix it for all imag extensions like jpg, gif, png, bmp ? – Dilip Feb 10 '16 at 01:00
  • Just need fix for all image extensions and rename all the images to Capitalize format example: Lenovo-Wallpaper-Xaf454ab.jpg – Dilip Feb 10 '16 at 01:16
  • @Dilip See edit. Only capitalises first letter, you'll have to find another solution if you want it to capitalise more than that. – Jonno Feb 10 '16 at 06:11
  • Ok. I will give it a try and let you know. Thanks a lot to you to solve the problem. I tried many renaming tools but didn't work. Thanks to you... – Dilip Feb 10 '16 at 09:45
  • Hello, Please ignore capitalize functionality.. Just help me fix all the image formats to get renamed. – Dilip Feb 11 '16 at 13:01
  • @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! goto :retry endlocal & ren %1 %~n1-%name% – Dilip Feb 11 '16 at 13:01
  • The above code is removing file extension.. Help me fix file extension get nochange... – Dilip Feb 11 '16 at 13:02