Zufallsdatei öffnen per Batch Datei

Hey,

ich möchte gerne aus einem Ordner eine Zufallsdatei öffnen.

Ich hab einen Batch Code gefunden

    @echo off & setlocal

  :: start of main

  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

Allerdings befindet sich in dem Ordner bei mir nicht nur Datein, sondern auch andere Unterordner mit weiteren Dateien. Wie kann ich diese miteinbeziehen?

Vielen Dank :slight_smile:

@echo off
setlocal

:: Create numbered list of files in a temporary file
set "tempFile=%temp%\%~nx0_fileList_%time::=.%.txt"
dir /b /s /a-d %1 | findstr /n "^" >"%tempFile%"

:: Count the files
for /f %%N in ('type "%tempFile%" ^| find /c /v ""') do set cnt=%%N

call :openRandomFile

:: Delete the temp file
del "%tempFile%"

exit /b

:openRandomFile
set /a "randomNum=(%random% %% cnt) + 1"
for /f "tokens=1* delims=:" %%A in (
  'findstr "^%randomNum%:" "%tempFile%"'
) do start "" "%%B"
exit /b

Vielen Dank :slight_smile: