Batch file 从随机文件选择中排除某些文件扩展名-批处理脚本
我正在制作一个脚本,从用户指定的文件夹中选择25首随机曲目,并将它们添加到.m3u播放列表中。我遇到的问题是,我的音乐文件夹还包括各种其他文件(例如:.txt、.jpg、.png、.nfo等) 因此,我正在寻找一种排除上面列出的扩展的方法,将脚本限制为只处理音频文件。任何帮助都将不胜感激 这是到目前为止的代码。。。它是从各种来源拼凑而成的,所以如果有点粗糙,请接受我的道歉:Batch file 从随机文件选择中排除某些文件扩展名-批处理脚本,batch-file,playlist,Batch File,Playlist,我正在制作一个脚本,从用户指定的文件夹中选择25首随机曲目,并将它们添加到.m3u播放列表中。我遇到的问题是,我的音乐文件夹还包括各种其他文件(例如:.txt、.jpg、.png、.nfo等) 因此,我正在寻找一种排除上面列出的扩展的方法,将脚本限制为只处理音频文件。任何帮助都将不胜感激 这是到目前为止的代码。。。它是从各种来源拼凑而成的,所以如果有点粗糙,请接受我的道歉: @echo off title Multiple Choice Menu :home cls echo. echo Sel
@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
)
假设您的歌曲文件夹位于
%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%
。正如我所见,由于选择文件的代码在这个文件上运行,所以它根本不需要更改
我无法使dir同时使用目标目录和通配符
希望能有帮助。
这就是我使用and的原因。假设您的歌曲文件夹位于
%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%
。正如我所见,由于选择文件的代码在这个文件上运行,所以它根本不需要更改
我无法使dir同时使用目标目录和通配符
希望能有帮助。
这就是我使用和的原因。谢谢丹尼尔
我又试了一次,终于有了自己的办法。我使用了一系列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
也许这不是最干净的方法,但是,嘿,它很管用 谢谢丹尼尔
我又试了一次,终于有了自己的办法。我使用了一系列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
也许这不是最干净的方法,但是,嘿,它很管用 更换
dir /b /s /a-d %1 | findstr /n "^" >"%tempFile%"
与
其中,/c:“.mp3”…
可以根据您选择的目标文件类型进行多次扩展
或
与
再次重复/c:“.jpg”…
选择目标文件类型的次数
/i
表示“不区分大小写”;/e
表示“在行尾”/L
表示“文字匹配,而不是正则表达式”;/v
表示“输出不匹配”替换
dir /b /s /a-d %1 | findstr /n "^" >"%tempFile%"
与
其中,/c:“.mp3”…
可以根据您选择的目标文件类型进行多次扩展
或
与
再次重复/c:“.jpg”…
选择目标文件类型的次数
/i
表示“不区分大小写”;/e
表示“在行尾”/L
表示“文字匹配,而不是正则表达式”;/v
表示“输出不匹配”