Bat file to open at ramdom all but 2 files in a folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
snerkRsnelt
Posts: 5
Joined: 17 Jan 2012 23:10

Bat file to open at ramdom all but 2 files in a folder

#1 Post by snerkRsnelt » 17 Jan 2012 23:21

Here is what I have so far it random opens 1 file at a time

@echo off & setlocal

rem Set your path here:
set "workDir=C:\Users\file_test"

rem %random%
rem %random%
@set /a "rdm=%random%"
set /a "rdm=%random%"

rem Push to your path.
pushd "%workDir%"

rem Count all files in your path. (dir with /b shows only the filenames)
set /a "counter=3"
for /f "delims=" %%i in ('dir /b ^|find "."') do call :sub1

rem This function gives a value from 1 to upper bound of files
set /a "rdNum=(%rdm%*%counter%/32767)+1"

rem Start a random file
set /a "counter=0"
for /f "delims=" %%i in ('dir /b ^|find "."') do set "fileName=%%i" &call :sub2

rem Pop back from your path.
popd "%workDir%"

goto :eof
:: end of main

:: start of sub1
:sub1
rem For each found file set counter + 1.
set /a "counter+=1"
goto :eof
:: end of sub1

:: start of sub2
:sub2
rem 1st: count again,
rem 2nd: if counted number equals random number then start the file.
set /a "counter+=1"
if %counter%==%rdNum% (start "" "%fileName%")
goto :eof

I found this online Sorry for been a noob but i really need help

Aacini
Expert
Posts: 1930
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Bat file to open at ramdom all but 2 files in a folder

#2 Post by Aacini » 17 Jan 2012 23:55

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Set your path here:
set "workDir=C:\Users\file_test"
rem Push to your path
pushd "%workDir%"
rem Create a vector with all files in your path
set i=0
for %%f in (*.*) do (
   set /A i+=1
   set file!i!=%%f
)
rem Remove a random file from the vector
set /A rdNum=%random%*i/32768+1
set file%rdNum%=
rem Remove a second random file from the vector (not the same)
:remSecond
   set /A rdNum=%random%*i/32768+1
   if not defined file%rdNum% goto remSecond
set file%rdNum%=
rem Start the remaining files (all but two)
for /L %%i in (1,1,%i%) do if defined file%%i start "File %%i" !file%%i!
rem Pop back to your path
popd

snerkRsnelt
Posts: 5
Joined: 17 Jan 2012 23:10

Re: Bat file to open at ramdom all but 2 files in a folder

#3 Post by snerkRsnelt » 18 Jan 2012 13:40

Thanks this has worked great. I am still only learning about how usefull bat files can be. . is it possable to set a short delay between each program opened ?

aGerman
Expert
Posts: 4740
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Bat file to open at ramdom all but 2 files in a folder

#4 Post by aGerman » 18 Jan 2012 14:39

Code: Select all

for /L %%i in (1,1,%i%) do if defined file%%i (
  start "File %%i" !file%%i!
  REM ping -n (number of seconds +1) localhost >nul
  ping -n 2 localhost >nul
)

Regards
aGerman

snerkRsnelt
Posts: 5
Joined: 17 Jan 2012 23:10

Re: Bat file to open at ramdom all but 2 files in a folder

#5 Post by snerkRsnelt » 18 Jan 2012 15:07

is it possable to make less than one second delay

snerkRsnelt
Posts: 5
Joined: 17 Jan 2012 23:10

Re: Bat file to open at ramdom all but 2 files in a folder

#6 Post by snerkRsnelt » 18 Jan 2012 15:13

or have one process open only after the previous has finished

aGerman
Expert
Posts: 4740
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Bat file to open at ramdom all but 2 files in a folder

#7 Post by aGerman » 18 Jan 2012 15:42

snerkRsnelt wrote:or have one process open only after the previous has finished

Have a look at the help message of START ( start /? ).

Code: Select all

start "File %%i" /wait !file%%i!

Regards
aGerman

snerkRsnelt
Posts: 5
Joined: 17 Jan 2012 23:10

Re: Bat file to open at ramdom all but 2 files in a folder

#8 Post by snerkRsnelt » 18 Jan 2012 22:26

thanks it works great you the man

Post Reply