【发布时间】:2016-11-05 01:16:51
【问题描述】:
我想创建一个批处理文件,用于打开文件夹及其所有子文件夹中的随机文件。
我还想添加“例外文件夹” - 随机打开的文件夹。
目前,我正在使用这个:
@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
它非常适合随机打开,但我不确定如何排除特定目录中的一些子文件夹。
提前感谢您的帮助! :)
【问题讨论】:
-
将 robocopy 与 /L 和 /XD 开关一起用于您的编号列表
标签: batch-file random