Bat file to open at ramdom all but 2 files in a folder
Moderator: DosItHelp
-
- Posts: 5
- Joined: 17 Jan 2012 23:10
Bat file to open at ramdom all but 2 files in a folder
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
@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
Re: Bat file to open at ramdom all but 2 files in a folder
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
-
- Posts: 5
- Joined: 17 Jan 2012 23:10
Re: Bat file to open at ramdom all but 2 files in a folder
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 ?
Re: Bat file to open at ramdom all but 2 files in a folder
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
-
- Posts: 5
- Joined: 17 Jan 2012 23:10
Re: Bat file to open at ramdom all but 2 files in a folder
is it possable to make less than one second delay
-
- Posts: 5
- Joined: 17 Jan 2012 23:10
Re: Bat file to open at ramdom all but 2 files in a folder
or have one process open only after the previous has finished
Re: Bat file to open at ramdom all but 2 files in a folder
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
-
- Posts: 5
- Joined: 17 Jan 2012 23:10
Re: Bat file to open at ramdom all but 2 files in a folder
thanks it works great you the man