start random file
Moderator: DosItHelp
start random file
hey guys... i need a simple thing...
when i start a bat file, i need to do this:
1) shutdown -s -t 1800 -f
2) start a random file from "E:/Series/Red Dward/" (its all avi files, but u cant use %random% as the files have spaces and special chars in name....
its gonna be so simple for ya, thanks for your help guys
when i start a bat file, i need to do this:
1) shutdown -s -t 1800 -f
2) start a random file from "E:/Series/Red Dward/" (its all avi files, but u cant use %random% as the files have spaces and special chars in name....
its gonna be so simple for ya, thanks for your help guys
Re: start random file
This might work. I cobbled it together from an example I had squirreled away.
Code: Select all
@echo off
shutdown -s -t 1800 -f
pushd "E:\Series\Red Dwarf\"
dir "*.avi" /a-d /b >"%temp%\tmp.tmp"
for /f %%a in ('find /c /v "" ^< "%temp%\tmp.tmp"') do set lines=%%a
set /a rand=%RANDOM% %% %lines%
if [%rand%]==[0] (set var=) else (set var=skip=%rand%)
for /f "%var% tokens=*" %%a in ('type "%temp%\tmp.tmp"') do (
set "text=%%a"
goto :continue
)
:continue
set /a line=rand+1
:: echo line %line% of %lines% : "%text%"
start "" "%text%"
Re: start random file
thanks man, first try i worked well
when i tried another time, it just opened a cmd windows, and was blank, didnt set the shutdown and didnt open the file eather...
any ideas?
when i tried another time, it just opened a cmd windows, and was blank, didnt set the shutdown and didnt open the file eather...
any ideas?
Re: start random file
haha good one
cant you please have a check whats wrong?:(
cant you please have a check whats wrong?:(
Code: Select all
@echo off
shutdown -s -t 1800 -f
pushd "E:\Series\red dwarf\"
dir "*.m4v" /a-d /b >"%temp%\tmp.tmp"
for /f %%a in ('find /c /v "" ^< "%temp%\tmp.tmp"') do set lines=%%a
set /a rand=%RANDOM% %% %lines%
if [%rand%]==[0] (set var=) else (set var=skip=%rand%)
for /f "%var% tokens=*" %%a in ('type "%temp%\tmp.tmp"') do (
set "text=%%a"
goto :continue
)
:continue
set /a line=rand+1
:: echo line %line% of %lines% : "%text%"
start "" "%text%"
Re: start random file
The random file selector function works here.
It lists the files and picks a random one and launches it using system file associations. The shutdown command is your syntax that I copied and pasted.
You changed the avi filetype I noticed - change it back to avi and see if it works - it could be a bug in your media player.
It lists the files and picks a random one and launches it using system file associations. The shutdown command is your syntax that I copied and pasted.
You changed the avi filetype I noticed - change it back to avi and see if it works - it could be a bug in your media player.
Re: start random file
ok figured out...
tried without shutdown command, and worked
so i tried to put the shutdown command on the end of the file, and works fine now...
just wondering why it worked for the first time you game me the script, and then it didnt...
but as ive put it in the end, works fine so fat
thanks bro
tried without shutdown command, and worked
so i tried to put the shutdown command on the end of the file, and works fine now...
just wondering why it worked for the first time you game me the script, and then it didnt...
but as ive put it in the end, works fine so fat
thanks bro
Re: start random file
This is a different (simpler and faster) solution that use an array instead of a temp file.However, if file names may include an exclamation mark a small modification is required.
Antonio
Code: Select all
@echo off
setlocal EnableDelayedExpansion
cd "E:\Series\Red Dwarf\"
set i=0
for %%f in (*.avi) do (
set /A i+=1
set "file[!i!]=%%a"
)
set /A rand=(%random%*i)/32768 + 1
start "" "!file[%rand%]!"
shutdown -s -t 1800 -f
Antonio
Re: start random file
Aacini wrote:@echo off
setlocal EnableDelayedExpansion
cd /d "E:\Series\Red Dwarf\"
set i=0
for %%a in (*.avi) do (
set /A i+=1
set "file[!i!]=%%a"
)
set /A rand=(%random%*i)/32768 + 1
start "" "!file[%rand%]!"
shutdown -s -t 1800 -f
However, if file names may include an exclamation mark a small modification is required.
With the two changes above, it still picks the first file in the folder every time.
Code: Select all
@echo off
setlocal EnableDelayedExpansion
:: cd /d "E:\Series\Red Dwarf\"
set i=0
for %%a in (*.*) do (
set /A i+=1
set "file[!i!]=%%a"
)
set /A rand=(%random%*i)/32768 + 1
echo %rand%
echo :: start "" "!file[%rand%]!"
set file
pause
This is what I get every time I invoke it. 3 is returned each time. (filenames trunccated below)
3
:: start "" "b"
file[10]=z.bat
file[1]=a
file[2]=A.BAT
file[3]=b
file[4]=colour c
file[5]=colourXP
file[6]=random-a
file[7]=remove.t
file[8]=Specs -
file[9]=Specs -
Re: start random file
Hi Aacini, it's been a while...
You get the idea.
I've tested Aacini's random implementation succesfully, the max ( 2147483647 ) is well over 32768 * 32768.
I prefer modulus as random returns only 32768 distinct numbers anyways.
That's what you said last time to that other guy too, why not just modify it straight away, and play those exotic files, they are quite common for AVI's.Aacini wrote:However, if file names may include an exclamation mark a small modification is required.
Code: Select all
@echo off &setlocal disableDelayedExpansion
::cd /d "E:\Series\Red Dwarf\"
set "$=" &for %%? in (
*.AVI
) do set /a $ += 1 &for /f %%@ in (
'call echo.%%$%%'
) do set "$file[%%@]=%%?"
::
set /a $random = %random% %% $ + 1
set $
::notepad.EXE "%file[1]%"
::notepad.EXE "%file[2]%"
::notepad.EXE "%file[3]%"
call echo.start "" /wait notepad.EXE "%%$file[%$random%]%%"
call start "" /wait notepad.EXE "%%$file[%$random%]%%"
exit
::shutdown -s -t 1800 -f
Code: Select all
$=3
$file[1]=&Tekstdocument.AVI
$file[2]=Nieuw - Tekstdocument^.AVI
$file[3]=Tekstdocument!.AVI
$random=2
start "" /wait notepad.EXE "Nieuw - Tekstdocument^.AVI"
Weird;foxidrive wrote:This is what I get every time I invoke it. 3 is returned each time.
I've tested Aacini's random implementation succesfully, the max ( 2147483647 ) is well over 32768 * 32768.
I prefer modulus as random returns only 32768 distinct numbers anyways.
Re: start random file
hey guys, back after 3 years
so ive been using this past 3 years, all worked fine...
but just last night i fresh installed win7 again (as i was doing several times in those 3 years) all updates are up as usual...
but cant understand the reason, the code doesnt work anymore...
once u run it... its trying to run all files from the directory again and again...
when i just try to
dos window keeps listing names of all files in the directory in random order (not just each file once, but in a random loop all over and over again)
so how do i stop the loop once it find a random file from that?
thanks for reply
Code: Select all
@echo off
pushd "C:\Ruzne\Red Dwarf\"
dir "*.avi" /a-d /b >"%temp%\tmp.tmp"
for /f %%a in ('find /c /v "" ^< "%temp%\tmp.tmp"') do set lines=%%a
set /a rand=%RANDOM% %% %lines%
if [%rand%]==[0] (set var=) else (set var=skip=%rand%)
for /f "%var% tokens=*" %%a in ('type "%temp%\tmp.tmp"') do (
set "text=%%a"
goto :continue
)
:continue
set /a line=rand+1
:: echo line %line% of %lines% : "%text%"
start "" "%text%
shutdown -s -t 1800 -f
so ive been using this past 3 years, all worked fine...
but just last night i fresh installed win7 again (as i was doing several times in those 3 years) all updates are up as usual...
but cant understand the reason, the code doesnt work anymore...
once u run it... its trying to run all files from the directory again and again...
when i just try to
Code: Select all
echo line %line% of %lines% : "%text%"
dos window keeps listing names of all files in the directory in random order (not just each file once, but in a random loop all over and over again)
so how do i stop the loop once it find a random file from that?
thanks for reply
Re: start random file
You called it shutdown.bat