10

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?

techdaemon
  • 215
  • 2
  • 7
  • 13

5 Answers5

17

(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
Joey
  • 344,408
  • 85
  • 689
  • 683
  • 1
    i entered `Get-ChildItem C:\temp\source | Get-Random -Count 100 | Move-Item -Destination C:\temp\output` into powershell console but nothing happened – techdaemon Apr 05 '11 at 16:14
  • @Joey, I tried this in PowerShell because I'm trying to get a script to randomly change my Log-on screen. My source directory was several folders deep in my user directory, and for some reason the Move-Item command threw an error when I tried to execute. It said `Cannot find path 'C:\Users\\' because it does not exist.` Basically it found a random file in my source directory, but then it screwed up the path, pointing to a nonexistent file in the top level of my user directory. Any idea why? – James Martineau Dec 10 '12 at 13:54
  • @rironin, I edited Joey's original script to fix the error. When getting the items from the folder, we need to get the FullName of the file, before passing it to Get-Random – Oleg D. Jan 12 '13 at 18:37
3

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
      )
RB.
  • 36,301
  • 12
  • 91
  • 131
  • what i did was copy the code to the powershell console. i'm afraid it didn't work. it gave me errors that are in red text. – techdaemon Apr 05 '11 at 13:47
  • Hi techdaemon - The code I posted was Batch, which is a completely different language from PowerShell. I've updated the Batch code and it should work now, as long as you use "/v:on" as I state. – RB. Apr 05 '11 at 13:48
  • It's a batch file. why did you copy it to powershell console and expect it to work? – kurumi Apr 05 '11 at 13:49
  • the "SrcCount" is where i specify the number of files to pick, isn't it? – techdaemon Apr 05 '11 at 13:51
  • No - that's a counter so it knows how many files it has copied. SrcMax is the number of files to copy. I've set it be a random number as that's what the question asked for. – RB. Apr 05 '11 at 13:53
  • @RB I need something where I can specify the number of random files that will be selected. I'm sorry if you misunderstood the question. @kurumi Pardon me for my naivete. – techdaemon Apr 05 '11 at 13:56
  • @techdaemon Do you want to be able to specify the number of files, or specify the range of a random number of files? – RB. Apr 05 '11 at 14:01
  • @RB I want to be able to specify the number of files please. – techdaemon Apr 05 '11 at 14:04
  • In that case, simply set SrcMax to be the value you want. E.g. `SET SrcMax=25` to copy 25 files. – RB. Apr 05 '11 at 14:10
  • Thanks, but still it does not work. i saved the code to something.bat and double clicked it but no files appeared in C:\temp\output. I made sure I copied my source files to C:\temp\source\. And about `cmd \v:on` i don't understand how it works with the rest of the code you wrote. Again, sorry for my naivete. – techdaemon Apr 05 '11 at 14:16
  • Click "Start" -> "Run" and type in `cmd \v:on`. This will launch a command prompt. Now run your batch file **inside** that command prompt. It should work ok. Obviously, you will need to set the source and output folders to something on your machine, and make sure you've updated `SrcMax`. – RB. Apr 05 '11 at 14:23
  • Still doesn't work. It gives me many "The system cannot find the file specified" error. But the filenames are being shown. I'm not sure if the reason is the spaces and special characters in the filenames. – techdaemon Apr 05 '11 at 14:33
  • (a) It should be `cmd /v:on` (a backslash is not a slash). (b) what you're written there will *only* work in the interactive shell, not in a batch file. (c) You probably want a few quotes to properly handle file names with spaces. – Joey Apr 05 '11 at 15:21
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
: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
T.Todua
  • 53,146
  • 19
  • 236
  • 237
1
@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

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 Enter key and then execute below code

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

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