使用批处理脚本从文件夹和子文件夹打开随机文件(Windows 7)

使用批处理脚本从文件夹和子文件夹打开随机文件(Windows 7)

我想获取一个 bat 文件,该文件将打开特定文件夹中的随机文件(具有任何扩展名),但也会打开该文件夹内所有子文件夹中的文件。还有另一个问题问了类似这样的问题(如何打开文件夹中的随机文件,并设置仅打开具有指定文件扩展名的文件?),并提供了此脚本:

@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

来源:http://forums.majorgeeks.com/showthread.php?t=181574

但是,此脚本仅打开主文件夹中的文件,而不打开子文件夹中的文件。我确信修复很简单,但我搞不懂。非常感谢您的帮助,谢谢。

答案1

此代码不仅可以随机打开文件夹层次结构中任何位置的文件,而且比原始代码更高效:

@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

默认情况下,脚本将在当前目录下查找文件,但您可以将根路径作为第一个参数传递,然后它就会从那里开始查找。

当只打开一个文件时,该代码效率更高,但如果您想打开多个文件,它确实会有所改进,因为它只需要生成一次列表。让 FINDSTR 查找选定的文件而不是循环遍历整个列表也更有效率。

我构造了代码,以便轻松打开多个随机文件。下面我随机选择了 25 个文件,并打印出打开它们的命令。只需删除 ECHO 即可实际打开文件:

@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

:: Open 25 random files
for /l %%N in (1 1 25) do 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 echo start "" "%%B"
exit /b

答案2

虽然我会使用 dbenham 答案中的代码,但这只是一种替代方案

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "rootFolder=C:\DVDCOVERS" 

    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"

代码的工作方式如下:

  1. 执行递归dir命令。
  2. 对于列表中的每个文件,回显文件的名称和随机数。
  3. 对文件列表进行排序。由于它以随机数作为前缀,因此顺序是随机的。
  4. 从列表中检索第一个文件。
  5. 检索到的记录被拆分,丢弃初始随机数并开始选定的文件。

是的,它是 CPU 密集型的,因为为了使其工作,需要启动一个sort命令和四个实例。cmd

相关内容