1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Batch program - How could i open a random image from a specified folder?

Discussion in 'Software' started by syeuk2002, Feb 5, 2009.

  1. syeuk2002

    syeuk2002 Private E-2

    Here is what i am trying to do...

    I have all of the covers for my DVD's in a folder on the PC. In order to decided what to watch i flick through these images.

    I was wondering, say if i have all the dvd covers in the folder C:\DVDCOVERS

    Could i then write a batch file to open a random image file?

    I have found 1 method on-line but it requires that i rename all of the images as numbers. I don't really want to do that.

    Please help!!

    Thanks x
     
  2. PC-XT

    PC-XT Master Sergeant

    You could have a list of them in a file (or generate one with dir) and try possibly using a variation of that method you found to find a particular file name to show by taking that line of the file?
     
  3. GermanOne

    GermanOne Major Geek's Coding Genius Extraordinaire

    Hi, you can try this:

    :: --snip--- start of batch

    @echo off & setlocal
    :: start of main
    rem Set your path here:
    set "workDir=C:\DVDCOVERS"

    rem Read the %random%, two times is'nt a mistake! Why? Ask Bill.
    rem In fact at the first time %random% is nearly the same.
    @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=0"
    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
    :: end of sub2

    :: -snap--- end of batch

    Hope that helps and regards from Germany.
     
  4. Massimo73

    Massimo73 Private E-2

    For an avi file this would be the batch i use, you can change the extension or use %1 and pass it as an argument.

    Cheers,
    Massimo


    :: --snip--- start of batch
    @echo off
    setlocal enableDelayedExpansion
    :: start of main


    rem Read the random, two times is'nt a mistake! Why? Ask Bill.
    rem In fact at the first time random is nearly the same.
    @set "rdm=%random%"
    set "rdm=%random%"

    ::build "array" of files
    set fileCnt=0
    for /f "delims=" %%i in ('dir /b /s *.avi *.mpg *.wmv *.mkv *.mp4 *.mov *.m4v') do (
    set /a fileCnt+=1
    set "fileArray!fileCnt!=%%i"
    )

    rem This function gives a value from 1 to upper bound of files
    set /a rdNum=(%rdm%*%fileCnt%/32767)+1
    echo "total number of files = %fileCnt%"
    echo "random files # = %rdNum%"

    rem Start a random file
    call :selectOneFile

    goto :eof
    :: end of main


    :: start of selectOneFile
    :selectOneFile

    set fileName=!fileArray%rdNum%!
    echo %fileName%
    start "" "%fileName%"
    pause
    exit
    )

    goto :eof
    :: end of selectOneFile

    :eof
    :: -snap--- end of batch
     

Share This Page

MajorGeeks.Com Menu

MajorGeeks.Com \ All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ NEW! PC Games \ System Tools \ Macintosh \ Demonews.Com \ Top Downloads

MajorGeeks.Com \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds