callmecheez
asked on
batch file - randomly copy 1 of 10 files from location A to B?
Need help with a batch file that will - at random - select a file from a directory of files, and copy it to a set location?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I didn't know there was a %random% in DOS - you learn something new every day? :-)
Interesting question...
The following code calculates the number of files in the folder and then generates a random number based on this value so that you can be sure every file is properly accounted for.
This value is then used to generate a random number in the correct range (1...# files) which is used to locate a corresponding random file.
Strictly speaking, this would be the proper and formal way to achieve what you are attempting to do.
INSTRUCTIONS
1) Copy and paste the code into Notepad.
2) Edit lines 3 and 4 to point to your own source and destination folders. Do not include a trailing '\'.
3) Save the code as a batch file giving it an appropriate name such as CPYRNDF.BAT for example.
4) Fire up a DOS box and enter the following command:
CPYRNDF
(or whatever you named your batch file)
NOTES
You could actually run the batch file directly from your AUTOEXEC.BAT file, or as part of Windows' startup or even under Windows' scheduler.
I have not included a filespec. This could be something like *.* or *.txt or whatever. If you need to be specific about the filespec then this can be added in both FOR statements replacing: dir /a-d /b "%source%" with something like: dir /a-d /b "%source%\*.txt".
CODE
@echo off
setlocal enabledelayedexpansion
set source=c:\temp\source folder
set destination=c:\temp\destin ation folder
for /f "tokens=1" %%a in ('dir /a-d "%source%" ^| findstr "File(s)"') do (
set /a rnd=!random! * %%a / 32767 + 1
for /f "tokens=1-2 delims=[]" %%b in ('dir /a-d /b "%source%" ^| find /n /v "" ^| find "[!rnd!]"') do (
copy "%source%\%%c" "%destination%\"
)
)
The following code calculates the number of files in the folder and then generates a random number based on this value so that you can be sure every file is properly accounted for.
This value is then used to generate a random number in the correct range (1...# files) which is used to locate a corresponding random file.
Strictly speaking, this would be the proper and formal way to achieve what you are attempting to do.
INSTRUCTIONS
1) Copy and paste the code into Notepad.
2) Edit lines 3 and 4 to point to your own source and destination folders. Do not include a trailing '\'.
3) Save the code as a batch file giving it an appropriate name such as CPYRNDF.BAT for example.
4) Fire up a DOS box and enter the following command:
CPYRNDF
(or whatever you named your batch file)
NOTES
You could actually run the batch file directly from your AUTOEXEC.BAT file, or as part of Windows' startup or even under Windows' scheduler.
I have not included a filespec. This could be something like *.* or *.txt or whatever. If you need to be specific about the filespec then this can be added in both FOR statements replacing: dir /a-d /b "%source%" with something like: dir /a-d /b "%source%\*.txt".
CODE
@echo off
setlocal enabledelayedexpansion
set source=c:\temp\source folder
set destination=c:\temp\destin
for /f "tokens=1" %%a in ('dir /a-d "%source%" ^| findstr "File(s)"') do (
set /a rnd=!random! * %%a / 32767 + 1
for /f "tokens=1-2 delims=[]" %%b in ('dir /a-d /b "%source%" ^| find /n /v "" ^| find "[!rnd!]"') do (
copy "%source%\%%c" "%destination%\"
)
)
Ah! Just noticed you're only copying 1 of 10 files.... This is even simpler. You need:
@echo off
set source=c:\temp\source folder
set destination=c:\temp\destin ation folder
set /a rnd=!random! * 10 / 32767 + 1
for /f "tokens=1-2 delims=[]" %%a in ('dir /a-d /b "%source%" ^| find /n /v "" ^| find "[%rnd%]"') do (
copy "%source%\%%b" "%destination%\"
)
@echo off
set source=c:\temp\source folder
set destination=c:\temp\destin
set /a rnd=!random! * 10 / 32767 + 1
for /f "tokens=1-2 delims=[]" %%a in ('dir /a-d /b "%source%" ^| find /n /v "" ^| find "[%rnd%]"') do (
copy "%source%\%%b" "%destination%\"
)
Oops! Sorry, typo. I meant...
@echo off
set source=c:\temp\source folder
set destination=c:\temp\destin ation folder
set /a rnd=%random% * 10 / 32767 + 1
for /f "tokens=1-2 delims=[]" %%a in ('dir /a-d /b "%source%" ^| find /n /v "" ^| find "[%rnd%]"') do (
copy "%source%\%%b" "%destination%\"
)
@echo off
set source=c:\temp\source folder
set destination=c:\temp\destin
set /a rnd=%random% * 10 / 32767 + 1
for /f "tokens=1-2 delims=[]" %%a in ('dir /a-d /b "%source%" ^| find /n /v "" ^| find "[%rnd%]"') do (
copy "%source%\%%b" "%destination%\"
)
I believe 27654755 was a valid solution to the question.
~bp
~bp
HH:MM:SS.th, where HH=hour, MM=minute, SS=seconds, t=1/10th seconds, h=1/100th seconds
so it you use the %TIME% variable whenever you run the script and extract the h hundredths of
a second, you will have a reasonable random number between 0 and 9.
You can extract that number with the command:
set randomnum=%time:~10,1%
You should be able to use that number to select the one you want
eg:
copy file%randomnumber% <new_pathname>