

Tested on directories containing 8, 25, and 500 bmp files.Furthermore, bulk renaming saves time as well as allows for easier file management and organization. Set newfilename=%TEMPPREFIX%%NUM%.%FILEEXTENSION% Set /a num=(%random% %% %countfiles%) + 1 setlocalįor /f %%a in ('dir/b *.%FILEEXTENSION% ^| find /C "."') do set countfiles=%%aįor /L %%a in (1 1 %countfiles%) do call :RandomizeSingleFile %%aįor /L %%a in (1 1 %countfiles%) do ren %TEMPPREFIX%%%a.%FILEEXTENSION% %%a.%FILEEXTENSION% This could cost you a small amount of randomness, but once you have a solution, you can always run it multiple times.Īlso doesn't require enabledelayedexpansion, though I think I work at the only place left in the world that doesn't use it. It also uses a +1 slide so that it doesn't rely on %random% eventually hitting all possible values. Here's one that is independent of the count of files (though it does require them to be named 1.bmp to n.bmp).


That is, iterate over the list of folders and for each one call the randomize procedure. To make it work across subdirectories, the call line can be changed to something like for /r "%workingFolder%" /d %%d in (.) do call :randomizeFolderFiles "%%~fd" "%fileExtension%" To pass the file extension as argument to the batch file (or to directly set the value in the fileExtension variable) only the bmp is needed. So rename f (in the original list the file in position %%a) to f (the random selected file in the shuffle) In each iteration of the loop, the element %%a (with %%a from %count% to 0) is swapped with a random element !e! in the range 0.%%a, leaving in %%a the final element for this position of the array. The array a is shuffled using a Fisher-Yates in place shuffle.
BATCH FILE RENAME RANDOM FULL
The full list of files is readed in f, and the indexes in a. :randomizeFolderFiles folder fileExtensionĭir /b /a-d "*.%~2" 2^>nul ^| findstr /n "^" If not defined fileExtension set "fileExtension=*"Ĭall :randomizeFolderFiles "%workingFolder%" "%fileExtension%" If not defined workingFolder set "workingFolder=%cd%" Setlocal enableextensions disabledelayedexpansion There is no requirements in file name format, number of files (limited to environment memory) or special characters in file names. That means one rename operation for each file and one rename operation over the full list of files. This temporary extension is later removed. In this case, files are renamed one by one to a temporary extension to avoid collisions while processing the list. Read all the files into an array, and shuffle the array, renaming as the shuffle processes files. Coded as a subroutine uses a similar idea in answer. To execute this to all sub-directories (lets say the script is saved as randomizer.bat) use this with the actual path to the script: offįor /d /r %%# in (*) do call c:\randomizer.bat "%%~#" Now it accepts the directory as a first argument and counts the. (GMT + 0:00) - rename part fixed.ĮDIT edited according to the new requirements. I thought it will be madness to wait all the numbers to be filled only by waiting %random% to generate all numbers.So I've created :backward and :forward functions for faster generation.ĮDIT 10:05:57 p.m. Rem echo setting !inner_rnd! to %set_to% from :forward Rem echo setting !inner_rnd! to %set_to% from :backwardįor /l %%$ in (!down_limit!, 1, !files_count! ) do ( Rem echo setting !rnd! to %%# in outer function

For /f %%A in ('dir /a-d-s-h /b *.bmp ^| find /v /c ""') do set files_count=%%A
