1

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 answer here looks promising. [superuser.com/questions/349474/… [1]: superuser.com/questions/349474/… – Patrick Seymour Jul 3 '14 at 17:29
  • @PatrickS. That script does a great job at generating the random characters I need. Now to figure out how to tweak it to rename files. – J4204144 Jul 3 '14 at 18:19
  • I added REN *.jpg !_RndAlphaNum!.jpg to the end of the script. It is renaming like I want to but only one file at a time. How do I fix that? – J4204144 Jul 3 '14 at 18:31
  • 1
    Using both upper and lower case in a random name is pointless since Windows ignores case in file names: this.txt and THIS.TXT and tHiS.tXT are all equivalent file names. – dbenham Jul 3 '14 at 20:39
8

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
  • 1
    That worked perfectly. Huge thanks for the help – J4204144 Jul 5 '14 at 18:09
0
@echo off

REM The length of the random file name.
Set _RNDLength=8


REM Alphanumeric characters from which to choose.
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
SET _Str=%_Alphanumeric%987654321


REM Define the folder to be searched. This value will be used later
REM to build the full path to the file. This is necessary because we
REM are not using /S with the dir command.
SET folder=C:\Users\patrick.seymour\Pictures


REM Loop through all of the JPEGs in the specified folder.
for /f "tokens=*" %%a in ('dir /b "%folder%\*.jpg"') do (

SET RESULT=

SETLOCAL ENABLEDELAYEDEXPANSION
CALL :GetRandomName

echo Renaming "%folder%\%%a" to "!RESULT!.jpg"
ren "%folder%\%%a" "!RESULT!.jpg"
ENDLOCAL

)

GOTO :EOF

:GetRandomName
SET /A _Len=0
:_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
REM Echo Random string is !_RndAlphaNum!
SET RESULT=!_RndAlphaNum!
GOTO :EOF
REM EXIT /B

:EOF

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.