start random file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
shokarta
Posts: 15
Joined: 14 Oct 2012 05:54

start random file

#1 Post by shokarta » 14 Oct 2012 05:58

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 :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: start random file

#2 Post by foxidrive » 14 Oct 2012 06:13

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%"

shokarta
Posts: 15
Joined: 14 Oct 2012 05:54

Re: start random file

#3 Post by shokarta » 14 Oct 2012 14:01

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?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: start random file

#4 Post by foxidrive » 14 Oct 2012 14:43

Try it again.

shokarta
Posts: 15
Joined: 14 Oct 2012 05:54

Re: start random file

#5 Post by shokarta » 14 Oct 2012 20:30

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%"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: start random file

#6 Post by foxidrive » 14 Oct 2012 23:22

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.

shokarta
Posts: 15
Joined: 14 Oct 2012 05:54

Re: start random file

#7 Post by shokarta » 15 Oct 2012 10:04

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

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

Re: start random file

#8 Post by Aacini » 28 Oct 2012 19:19

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: start random file

#9 Post by foxidrive » 29 Oct 2012 02:14

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 -

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: start random file

#10 Post by Ed Dyreen » 29 Oct 2012 04:15

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.

shokarta
Posts: 15
Joined: 14 Oct 2012 05:54

Re: start random file

#11 Post by shokarta » 10 Jul 2015 19:37

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: start random file

#12 Post by foxidrive » 10 Jul 2015 21:12

You called it shutdown.bat :)

Post Reply