我已经整理了一个小批量文件程序来创建播放列表
@echo off
DIR /S /o:n /b *.avi > Playlist.m3u
有没有办法改变它,以便每次运行时它会按随机顺序排序?
答案 0 :(得分:1)
可以做到,但它不会很漂亮!你能使用更好的平台而不是批处理文件吗?也许这只是你一直在等待学习Powershell的机会! : - )
但是,如果你坚持批量,这是我尝试的一般方法:
set /a randomLineNum=%random% %% 10
会将%randomLineNum%设置为0到9之间的数字。for /f "skip=%randomLineNum%" %%L in ('dir /s /o:n /b *.avi') ...
之类的内容来抓取该随机行,并使用echo %%L > Playlist.m3u
。这种简单化的方法最终会产生重复,而且我没有以任何方式构建退出循环。我留下这些问题给你解决(或在将来的问题中提问)。 : - )
答案 1 :(得分:0)
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
::
:: establish a tempfile name
::
:temploop
SET tempfile="%temp%\temp%random%.tmp"
IF EXIST %tempfile% GOTO temploop
::
:: Write the list of filenames with each
:: prefixed by a random number and a colon
::
(FOR /f "delims=" %%i IN (
'dir /s/b *.avi'
) DO ECHO !random!:%%i
)>%tempfile%
::
:: Write the playlist.
:: sort the tempfile (which places it
:: in random order) and remove the number:
::
(FOR /f "tokens=1*delims=:" %%i IN (
' sort ^<%tempfile% ') DO ECHO %%j
) >playlist.m3u
::
:: and delete the tempfile.
::
DEL %tempfile% 2>NUL
应该有效 - 如果您的文件/路径名包含!
代码中的文档。
答案 2 :(得分:0)
没有TEMP文件的解决方案:
@echo off &setlocal
set "playlist=Playlist.m3u"
del %playlist% 2>nul
set /a files=0
for %%i in (*.avi) do set /a files+=1
if %files% equ 0 (echo No AVI found&goto:eof) else echo %files% AVI's found.
set /a cnt=%files%-1
for /l %%i in (0,1,%cnt%) do for /f "delims=" %%a in ('dir /b /a-d *.avi^|more +%%i') do if not defined $avi%%i set "$avi%%i=%%a"
:randomloop
set /a rd=%random%%%%files%
call set "avi=%%$avi%rd%%%"
if not defined avi goto :randomloop
set "$avi%rd%="
>>%playlist% echo %avi%
set /a cnt-=1
if %cnt% geq 0 goto:randomloop
echo Done!
endlocal
这不使用DelayedExpansion,因此它可以处理名称中带有感叹号的文件。它需要更多时间,但也不需要临时文件。