2013-03-22 81 views
2

我已經把一個小的批處理文件程序來創建一個播放列表批處理文件播放列表隨機排序

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

有沒有辦法在每次運行時改變這種做法,它會以隨機的順序排序?

回答

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。

這種簡單的方法最終會出現重複,而且我沒有以任何方式構建退出循環。我將這些問題留給你解決(或者在未來的問題中提出)。 :-)

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 

應該努力 - 但它有困難,如果你的文件/路徑名包含在代碼!

文檔。

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,因此它可以在名稱中感嘆號處理文件。它需要更多時間,但也不需要臨時文件。