Josh
Josh

Reputation: 23

Playing a The First Video located in a random folder/subfolder

I would like to make a .bat file that will open the first file within a random folder/subfolders, in the same location as the .bat file.

The code I currently have only opens a random file.

@echo off
setlocal enableextensions disabledelayedexpansion

set "rootFolder=G:\Movies\Anime" 

for /f "usebackq tokens=1,* delims=:" %%a in (`
    cmd /q /v /e /c "for /f delims^= %%a in ('dir /a-d /s /b "%rootFolder%"') do echo(!random!:%%a"
    ^| sort 2^>nul
    ^| cmd /q /e /v /c "set /p ".^=" & echo(!.!"
`) do start "" "%%~b"

I also have a .bat file that generates a text file with a list of all folders in the same location. I'm not sure if it would be easier to reference that.

dir /b > Animelist.txt

Also if possible how to exclude it opening particular types of files such as jpegs / the other .bat file?

Upvotes: 2

Views: 115

Answers (2)

user6811411
user6811411

Reputation:

The random selection of a folder is unambiguous.
The first video file is a bit more difficult. If using dir with several extensions like @aschipfl suggested, this predetermines which extensions are looked for and found first.
The other way with excluding file types is more tedious without knowing which types might occur.

Here my batch Edited Streamlined some parts:

@echo off
setlocal enableextensions Enabledelayedexpansion
Set Cnt=0
Set "Exclude=.bat$ .cmd$ .jpg$ .jpeg$ .txt$"

Pushd "G:\Movies\Anime" 
(Echo::: Numbered List of folders
  For /f "delims=" %%F in (
    'Dir /B/S/AD/ON'
  ) Do Set /A Cnt+=1&Echo:!Cnt!:%%F 
)>DirList.txt

:: Get Random num 1..Cnt
Set /A RndDir=%Random% %% Cnt+1

:: Get random folder name
For /f "tokens=1,* Delims=:" %%F in (
  'Findstr "^%RndDir%:" DirList.txt '
) Do Set "DirName=%%G"
Echo selected %RndDir% of %Cnt% = folder %DirName%
Pushd "%DirName%

Set "FileName"
For /f "Delims=" %%F in (
  'Dir /B/A-D ^|findstr /i /V "%Exclude%"'
) Do If Not defined FileName Set "FileName=%%~F"
If defined FileName Start "" "%FileName%"
Popd
Popd
Goto :Eof

Upvotes: 1

MC ND
MC ND

Reputation: 70933

You can try with this

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "rootFolder=%cd%"
    set fileTypes= "*.avi" "*.mpeg" "*.mkv" 

    pushd "%rootFolder%" && (
        for /f "usebackq tokens=1,* delims=:" %%a in (`
            cmd /q /v /e /c "set p=&for /r %%a in (%fileTypes%) do if not !f!==%%~dpa (set f=%%~dpa&set /a ((%random% %% 16273^)+1^)*!random!&echo :%%~dpa)"
            ^| sort 2^>nul
            ^| cmd /q /e /v /c "set /p ".^=" && echo(!.!"
        `) do pushd "%%~b." && (
            for /f "delims=" %%c in ('
                dir /b /a-d /on %fileTypes% 2^>nul 
                ^| cmd /q /e /v /c "set /p ".^=" && echo(!.!"
            ') do start "" "%%~fc"
            popd
        )
        popd
    )

Decomposing the task in pieces

  • Configure where to search and what to search (first two set)
  • Change to the starting folder (pushd)
    • Execute a recursive search for the indicated file types and output the name of the folder where the file has been found (first cmd inside for /f
      • As more than one file can be found in the same folder, check that we will not output a duplicate element (if inside first cmd)
    • For each folder, generate a random number as a prefix (set /a) and output the folder (echo)
    • Sort the list on the random number (sort)
    • Get the first folder in the list. As the list is sorted on a random number, this folder has been random selected (second cmd inside for /f %%a)
    • Discard the random number and retrieve only the folder (the reason for the delims and tokens in the for /f %%a)
  • Change to the selected folder (pushd)
    • List the files of the indicated types inside the selected folder (dir)
    • From this list select only the first file (cmd)
    • Retrieve the selected file (for /f %%c)
    • Start the selected file (start)
    • Return to previous folder (popd)
  • Return to the starting folder (popd)

Upvotes: 1

Related Questions