2

我整理了一个小批量文件程序来创建播放列表

@echo off
DIR /S /o:n /b *.avi > Playlist.m3u

有没有办法改变它,以便每次运行时都会按随机顺序排序?

4

3 回答 3

1

可以,但不会很漂亮!您可以使用更好的平台而不是批处理文件吗?也许这正是您一直在等待学习 Powershell 的机会!:-)

但是,如果您坚持使用批处理,如果我要尝试,这是我会采取的一般方法:

  1. 计算文件夹中 .avi 文件的数量。
  2. 在 0 和这个数字之间选择一个随机数。例如,set /a randomLineNum=%random% %% 10将 %randomLineNum% 设置为 0 到 9 之间的数字。
  3. 使用类似的东西for /f "skip=%randomLineNum%" %%L in ('dir /s /o:n /b *.avi') ...来抓住那条随机线,然后echo %%L > Playlist.m3u.
  4. 回到#2。

这种简单的方法最终会导致重复,而且我没有以任何方式构建退出循环。我将这些问题留给您解决(或在以后的问题中提出)。:-)

于 2013-03-22T04:55:20.330 回答
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,因此它可以处理名称中带有感叹号的文件。这需要更多时间,但也不需要临时文件。

于 2013-03-22T09:11:59.377 回答
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

应该可以工作 - 但如果的文件/路径名包含!

代码中的文档。

于 2013-03-22T05:59:21.740 回答