Windows批处理:执行文件夹中所有文件的应用程序,随机排序



我使用媒体播放器经典播放视频。我可以制作一个批处理文件来播放播放列表,如下所示:

mpc.exe video1.avi video2.avi video3.avi ...

它有效。

现在我想制作一个批处理文件,用于播放随机排序的文件夹中的所有视频。

知道吗?

谢谢

下面的代码假设您正在播放当前目录中的文件,并且它假定您的.avi文件名都不以 = 开头。消除这些限制并不需要太多。

如果文件夹中的文件太多,则命令长度最终将超过允许的最大长度 8191 字节。

@echo off
setlocal disableDelayedExpansion
for /f "delims==" %%A in ('set file. 2^>nul') do set "%%A="
for /f "tokens=1* delims=:" %%A in (
  'dir /b /a-d *.avi^|findstr /n "^"'
) do (
  set "file.%%A=%%B"
  set "cnt=%%A"
)
set "cmd=mpc.exe"
for /l %%N in (%cnt% -1 1) do call :buildCmd %%N
%cmd%
exit /b
:buildCmd
set /a N=%random% %% %1
set "skip="
if %N% gtr 0 set "skip=skip=%N%"
for /f "%skip% tokens=1* delims==" %%A in ('set file.') do (
  set cmd=%cmd% "%%B"
  set "%%A="
  exit /b
)

如果任何视频文件的名称带有感叹号,则下面的批处理文件将失败。如果需要,可以修复此问题。

@echo off
setlocal EnableDelayedExpansion
rem Store the file names into an array
set cnt=0
for %%a in (*.avi) do (
   set /A cnt+=1
   set file[!cnt!]=%%a
)
rem Insert the names in the command line in random order
set cmd=mpc.exe
for /L %%a in (%cnt%,-1,1) do (
   set /A i=%%a*!random!/32768+1
   for %%i in (!i!) do set cmd=!cmd! "!file[%%i]!"
   set file[%%i]=!file[%%a]!
)
%cmd%

安东尼奥

相关内容

最新更新