[ACCEPTED]-windows batch file script to pick random files from a folder and move them to another folder-batch-file

Accepted answer
Score: 14

(I'm assuming that your X is known beforehand 3 – represented by the variable $x in 2 the following code).

Since you weren't adverse 1 to a PowerShell solution:

Get-ChildItem SomeFolder | Get-Random -Count $x | Move-Item -Destination SomeOtherFolder

or shorter:

gci somefolder | random -c $x | mi -dest someotherfolder
Score: 4

The following Batch code will do it. Note 6 that you will need to launch cmd using the 5 following command line:

cmd /v:on

to enable delayed 4 environment variable expansion. Note also 3 that it will pick a random number of files 2 from 0 to 32767 - you will probably want 1 to modify this part to fit your requirements!

SET SrcCount=0
FOR %F IN (C:\temp\source\*.*) DO IF !SrcCount! LSS %SrcMax% (
      SET /A SrcCount += 1
      ECHO !SrcCount! COPY %F C:\temp\output
      COPY %F C:\temp\output
Score: 2

here is a CMD code, which outputs the random file name (customize it to your needs):

@echo off & setlocal
set "workDir=C:\source\folder"
::Read the %random%, two times is'nt a mistake! Why? Ask Bill.
::In fact at the first time %random% is nearly the same.
@set /a "rdm=%random%"
set /a "rdm=%random%"
::Push to your path.
pushd "%workDir%"
::Count all files in your path. (dir with /b shows only the filenames)
set /a "counter=0"
for /f "delims=" %%i in ('dir /b ^|find "."') do call :sub1
::This function gives a value from 1 to upper bound of files
set /a "rdNum=(%rdm%*%counter%/32767)+1"
::Start a random file
set /a "counter=0"
for /f "delims=" %%i in ('dir /b ^|find "."') do set "fileName=%%i" &call :sub2
::Pop back from your path.
popd "%workDir%"
goto :eof
:: end of main
:: start of sub1
::For each found file set counter + 1.
set /a "counter+=1"
goto :eof
:: end of sub1
:: start of sub2
::1st: count again,
::2nd: if counted number equals random number then start the file.
set /a "counter+=1"
if %counter%==%rdNum% (
MSG * "%fileName%"
goto :eof
:: end of sub2


Score: 0
@echo off
setlocal EnableDelayedExpansion
cd \particular\folder
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%]!" \different\folder

from Need to create a batch file to select one random file from a folder and copy to another folder


Score: 0

A sample Powershell code that Moves 1000 Random files From C:\Test\A To C:\Test\B

$d = gci "C:\Test\A" | resolve-path  |  get-random -count 1000

Press 2 Enter key and then execute below code

Move-Item $d  -destination "C:\Test\B"

Don't forget 1 to add " mark before and after the path of the folders

More Related questions