我已经把一个小的批处理文件程序来创建一个播放列表批处理文件播放列表随机排序
@echo off
DIR /S /o:n /b *.avi > Playlist.m3u
有没有办法在每次运行时改变这种做法,它会以随机的顺序排序?
我已经把一个小的批处理文件程序来创建一个播放列表批处理文件播放列表随机排序
@echo off
DIR /S /o:n /b *.avi > Playlist.m3u
有没有办法在每次运行时改变这种做法,它会以随机的顺序排序?
它可以完成,但它不会很漂亮!你可以使用更好的平台而不是批处理文件吗?也许这只是你一直在等待学习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
。这种简单的方法最终会出现重复,而且我没有以任何方式构建退出循环。我将这些问题留给你解决(或者在未来的问题中提出)。 :-)
@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
应该努力 - 但它将有困难,如果你的文件/路径名包含在代码!
文档。
解决方案,而一个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,因此它可以在名称中感叹号处理文件。它需要更多时间,但也不需要临时文件。