Page 1 of 1
start random file
Posted: 14 Oct 2012 05:58
by shokarta
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
Re: start random file
Posted: 14 Oct 2012 06:13
by foxidrive
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
Posted: 14 Oct 2012 14:01
by shokarta
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?
Re: start random file
Posted: 14 Oct 2012 14:43
by foxidrive
Try it again.
Re: start random file
Posted: 14 Oct 2012 20:30
by shokarta
haha good one
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
Posted: 14 Oct 2012 23:22
by foxidrive
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.
Re: start random file
Posted: 15 Oct 2012 10:04
by shokarta
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
Re: start random file
Posted: 28 Oct 2012 19:19
by Aacini
This is a different (simpler and faster) solution that use an array instead of a temp file.
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
However, if file names may include an exclamation mark a small modification is required.
Antonio
Re: start random file
Posted: 29 Oct 2012 02:14
by foxidrive
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
Posted: 29 Oct 2012 04:15
by Ed Dyreen
Hi Aacini, it's been a while...
Aacini wrote:However, if file names may include an exclamation mark a small modification is required.
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.
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"
You get the idea.
foxidrive wrote:This is what I get every time I invoke it. 3 is returned each time.
Weird;
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
Posted: 10 Jul 2015 19:37
by shokarta
hey guys, back after 3 years
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
Posted: 10 Jul 2015 21:12
by foxidrive
You called it shutdown.bat