2016-02-16 62 views
1

我在作出该选择从用户指定的文件夹25个随机曲目,并将它们添加到播放列表m3u格式的脚本的过程。我遇到的麻烦是,我的音乐文件夹也包括各种其他文件(例如:.txt,.jpg,.png,.nfo等...)Exlcude某些文件扩展名 - 批处理脚本

所以我正在寻找一种排除上面列出的扩展名,限制脚本只能处理音频文件。任何帮助将非常感激!

这里是那么远,它已经从各种渠道缝合在一起,所以接受我的道歉,如果这是一个有点粗糙的代码:

@echo off 
title Multiple Choice Menu 
:home 
cls 
echo. 
echo Select a genre: 
echo ============= 
echo. 
echo 1) Jungle 
echo 2) D'n'B 
echo 3) Reggae 
echo 4) Hip-Hop 
echo 5) Exit 
echo. 
set /p genre=Type option: 
if "%genre%"=="1" cd /D "D:\NL Safe 2.0\High Quality\Jungle" 
if "%genre%"=="2" cd /D "D:\NL Safe 2.0\High Quality\DnB\Standard DnB" 
if "%genre%"=="3" cd /D "D:\NL Safe 2.0\High Quality\Reggae 
if "%genre%"=="4" cd /D "D:\NL Safe 2.0\High Quality\Hip-Hop" 
if "%genre%"=="5" exit 

:: Clear existing Playlist 
@echo. > C:\Users\Brew\Desktop\random.m3u 

:: Create numbered list of files in a temporary file 
set "tempFile=%temp%\%~nx0_fileList_%time::=.%.txt" 
dir /b /s /a-d %1 | findstr /n "^" >"%tempFile%" 

:: Count the files 
for /f %%N in ('type "%tempFile%" ^| find /c /v ""') do set cnt=%%N 

:: Open 25 random files 
for /l %%N in (1 1 25) do call :openRandomFile 

:: Delete the temp file 
del "%tempFile%" 

:openRandomFile 
set /a "randomNum=(%random% %% cnt) + 1" 
for /f "tokens=1* delims=:" %%A in (
    'findstr "^%randomNum%:" "%tempFile%"' 
) do (
    setlocal EnableDelayedExpansion 
    set tune=%%B 
    @echo !tune! >> C:\Users\Brew\Desktop\random.m3u 
) 

提前感谢!

回答

0

假设你的歌曲的文件夹是%1

dir命令支持的想法,

dir *.txt *.xml 

只列出.txt.xml文件通配符。

所以,你可以尝试做这个:

... 
:: Create numbered list of files in a temporary file 
set "tempFile=%temp%\%~nx0_fileList_%time::=.%.txt" 
pushd %1 
dir /b /s /a-d *.mp3 *.EXT1 *.EXT2 | findstr /n "^" >"%tempFile%" 
popd 
... 

EXT将要匹配的扩展。

这将只是你想要的文件创建%tempFile%。由于选择一个文件的代码ramdomly可以正常工作,正如我所见,通过这个文件,它不需要改变。

我不能让DIR与两个目标目录和通配符来工作。

希望它有帮助。 这就是为什么我用pushdpopd

0

谢谢丹尼尔,

我有另一个去它终于得到了我自己的东西。我用了一系列的if语句来把随机文件通过扩展验证检查

这是工作脚本

@echo off 
title Multiple Choice Menu 
:home 
cls 
echo. 
echo Select a task: 
echo ============= 
echo. 
echo 1) Jungle 
echo 2) D'n'B 
echo 3) Reggae 
echo 4) Hip-Hop 
echo 5) Exit 
echo. 
set /p genre=Type option: 
if "%genre%"=="1" cd /D "D:\NL Safe 2.0\High Quality\Jungle" 
if "%genre%"=="2" cd /D "D:\NL Safe 2.0\High Quality\DnB\Standard DnB" 
if "%genre%"=="3" cd /D "D:\NL Safe 2.0\High Quality\Reggae 
if "%genre%"=="4" cd /D "D:\NL Safe 2.0\High Quality\Hip-Hop" 
if "%genre%"=="5" exit 

:: Clear existing Playlist 
@echo. > C:\Users\Brew\Desktop\random.m3u 

:: Create numbered list of files in a temporary file 
set "tempFile=%temp%\%~nx0_fileList_%time::=.%.txt" 
dir /b /s /a-d %1 | findstr /n "^" >"%tempFile%" 

:: Count the files 
for /f %%N in ('type "%tempFile%" ^| find /c /v ""') do set cnt=%%N 

:openRandomFile 
echo opening random file..... 
set /a "randomNum=(%random% %% cnt) + 1" 
for /f "tokens=1* delims=:" %%A in (
    'findstr "^%randomNum%:" "%tempFile%"' 
) do (
    setlocal EnableDelayedExpansion 
    set tune=%%B 

FOR %%i IN ("!tune!") do call :CheckifMusic 
) 


:CheckifMusic 
echo checking now 
FOR %%i IN ("!tune!") DO (
SET fileextension=%%~xi 
IF !fileextension!==.aif goto ResultTrue 
IF !fileextension!==.flac goto ResultTrue 
IF !fileextension!==.m4a goto ResultTrue 
IF !fileextension!==.mp3 goto ResultTrue 
IF !fileextension!==.wav goto ResultTrue 
IF !fileextension!==.wma goto ResultTrue 

echo fail 
echo !fileextension! 
goto openRandomFile 


:ResultTrue 
echo pass 
echo !fileextension! 
@echo !tune! >> C:\Users\Brew\Desktop\random.m3u 
set /A COUNTER=COUNTER+1 
IF !COUNTER!==25 (
    echo finished 
    pause 
    goto Done 
) ELSE (
goto openRandomFile 
) 


:Done 
echo. 
:: Delete the temp file 
del "%tempFile%" 
exit 
) 
pause 

可能不是做最彻底的方法,但嘿 - 它的工作原理!

0

dir /b /s /a-d %1 | findstr /n /L /E /I /c:".mp3" /c:".wav">"%tempFile%" 

更换

dir /b /s /a-d %1 | findstr /n "^" >"%tempFile%" 

/c:".mp3"...可以根据您的需要选择目标文件类型扩展多次。

dir /b /s /a-d %1 | findstr /n /V /L /E /I /c:".jpg" /c:".txt">"%tempFile%" 

,你的愿望来选择目标再次重复/c:".jpg"...多次排除,我的文件类型。

/i装置“不区分大小写”,/e装置“在该行的末尾” /L指“文字的比赛,正则表达式不”和/v意思是“输出不匹配”