Copy a random file to another folder
I'm trying to create a batch file to copy a random file from one folder to another folder . but when I run the code below it copies the same file every time . does anyone know what's wrong with the code ? I have different types of files in the folder .jpg .txt .rtf I don't know if that is why it is not working.
@echo off setlocal EnableDelayedExpansion cd c:\users\paul\desktop\11\ set n=0 for %%f in (*.*) do ( set /A n+=1 set "file[!n!]=%%f" ) set /A "rand=(n*%random%)/32768+1" copy "!file[%rand%]!" c:\users\paul\desktop\12
Are you running this Batch file via a double-click from the explorer? When cmd.exe start execution it initialize the random seed with the current time, so if you execute the Batch file several times in a short lapse, and the number of different files to choose is small (like 50), the same file will be selected every time.
You may "randomize" the random number in several ways; this is one of the simplest methods:
for /L %%i in (1,1,%time:~-1%) do set "dummy=!random!"
Insert this line before the line that uses %random%.