Windows batch file script to select random files from a folder and move them to another folder

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

+4
source share
5 answers

(I assume that your X is known beforehand - represented by the variable $x in the following code).

Since you were not opposed to the PowerShell solution:

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

or shorter:

 gci somefolder | random -c $x | mi -dest someotherfolder 
+7
source

The following package code will do this. Note that you will need to run cmd using the following command line:

 cmd /v:on 

to enable advanced change of environment variables. Also note that it will select a random number of files from 0 to 32767 - you probably want to change this part to suit 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 %FC:\temp\output COPY %FC:\temp\output ) 
+3
source

there is a CMD code that displays the name of a random file (configure it to fit 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 :sub1 ::For each found file set counter + 1. set /a "counter+=1" goto :eof :: end of sub1 :: start of sub2 :sub2 ::1st: count again, ::2nd: if counted number equals random number then start the file. set /a "counter+=1" if %counter%==%rdNum% ( :: OUTPUT ALERT BOX with FILENAME MSG * "%fileName%" ) goto :eof :: end of sub2 
+2
source
 @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 You must create a batch file to select one random file from a folder and copy to another folder

0
source

I like the Powershell solution to get a given number of files and move them to a new folder. I had to look very long and hard for this. My problem is that I have about 60,000 files that I need to sort into new folders. My question is: how to create a loop using the powershell script and after each run the script creates a new folder for moving files until the source folder is empty and I have many folders with counted files? Thank you in advance.

0
source

Source: https://habr.com/ru/post/1346739/


All Articles