Batch file rename with random alphanumeric
I am looking to rename a bunch of images (all with .jpg extention) with random alphanumeric names. Example would be 08kDRH1s.jpg
or 0FwzUc3.jpg
. Doing some searching I did find a few batch scripts that used $RANDOM
but that is only values between 0 and 32767
A user pointed me to a batch script that generates random 8 character alphanumeric strings. Would it be hard to tweak this to rename files over just outputting the names to command line?
@Echo Off
Setlocal EnableDelayedExpansion
Set _RNDLength=8
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto _loop
Echo Random string is !_RndAlphaNum!
The following simple script will rename all .jpg files in the current folder to random 8 character alphanumeric names, preserving the .jpg extension. Note that it is possible for the random name generator to produce a name that already exists, so the script loops until it successfully generates a unique name.
@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
A bit more code yields a versatile utility that allows you to specify the source path and file mask, and also provides the option to process sub-directories as well. The utility always preserves the extension of each file. Be careful with this utility!
renameFilesRandom.bat
:: renameFilesRandom.bat [filter] [/s]
@echo off
setlocal disableDelayedExpansion
:: Parse and validate arguments
set "option="
set "filter="
if "%~3" neq "" (
>&2 echo ERROR: Too many arguments
exit /b 1
)
if /i "%~1" equ "/S" (set "option=/S") else if "%~1" neq "" set "filter=%~1"
if /i "%~2" equ "/S" (set "option=/S") else if "%~2" neq "" (
if defined filter (
>&2 echo ERROR: Only one filter allowed
exit /b 1
) else set "filter=%~2"
)
if "%filter:~0,1%" equ "/" (
>&2 echo ERROR: Invalid option %filter%
exit /b 1
)
if not defined filter set "filter=*"
:: Convert a directory filter into a file filter with wildcards
if exist "%filter%\" set "filter=%filter%\*"
:: Determine source if /S option not specified
set "src="
if not defined option for /f "eol=: delims=" %%F in ("%filter%") do set "src=%%~dpF"
:: Rename the specified files
set "chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for /f "eol=: delims=" %%F in ('dir /a-d /b %option% "%filter%"') do call :renameFile "%%F"
exit /b
:renameFile
setlocal
if not defined src set "src=%~dp1"
set "old=%~nx1"
set "ext=%~x1"
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!"
)
if exist "!src!!name!!ext!" goto :retry
ren "!src!!old!" "!name!!ext!"
Below are some sample usages.
Rename all files in the current directory:
renameFilesRandom
Rename all .jpg files in the current directory:
renameFilesRandom *.jpg
Rename all .jpg files in the c:\test folder, and all its sub-folders (recursive):
renameFilesRandom c:\test\*.jpg /s