Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

windows batch file script to pick random files from a folder and move them to another folder

I need a batch script to randomly select X number of files in a folder and move them to another folder. How do I write a windows batch script that can do this?

like image 399
techdaemon Avatar asked Apr 05 '11 13:04

techdaemon


2 Answers

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

cmd /v:on

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

@ECHO OFF
SET SrcCount=0
SET SrcMax=%RANDOM%
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
      )
like image 45
RB. Avatar answered Sep 18 '22 06:09

RB.


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

Since you weren't adverse 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
like image 192
Joey Avatar answered Sep 21 '22 06:09

Joey