有子文件夹包含批处理文件(mp3.bat)和此代码
@echo off
setlocal enabledelayedexpansion
chcp 65001
ffmpeg -version
if errorlevel 1 (
echo "ffmpeg not found"
@pause
exit
)
SET "filename=XXX - YYY"
echo "filename: %filename%"
echo "cd: %cd%"
dir
@pause
(FOR /R %%i IN (*.ts) DO @echo file 's/%%~nxi') > list.txt
ffmpeg -f concat -safe 0 -loglevel panic -i list.txt -c:a copy -vn "%filename%.mp3"
del "list.txt"
echo "success"
@pause
如何使用一个批处理文件从父目录执行每个 mp3.bat?
for /r "." %%a in (mp3.bat) do call "%%a"
它不起作用
答案1
类似这样的操作应该一次运行子文件夹中的每个批处理文件:
for /f %%a in ('dir /s /b mp3.bat') do start /wait "" "%%a"
或者
for /r %%a in (*mp3.bat) do call "%%a"
在每个文件夹中使用主批处理文件而不是 mp3.bat:
您必须将主文件夹(子文件夹所在的位置)拖放到此批处理文件中:
@echo off
chcp 65001 >nul
:: Please drag and drop the main folder where sub-folders are to this batch file
:Retry
if exist "%~1" (if not exist "%~1\" (set /p "Source=Please input source folder: ") else (set Source=%~1)) else (set /p "Source=Please input source folder: ")
if not exist "%Source%" goto :Retry
cd "%~dp0"
ffmpeg -version
if errorlevel 1 (
echo "ffmpeg not found"
@pause
exit
)
if exist "list.txt" del /q "list.txt"
cd "%Source%"
for /f "delims=" %%a in ('dir /b /ad *.*') do call :CreateMP3 "%%~dpnxa"
exit
:CreateMP3
cls
echo.
echo Processing Folder: %~nx1
pushd "%~1\s"
for /f "delims=" %%a in ('dir /b "*.ts"') do echo file '%%~a'>>"list.txt"
ffmpeg -f concat -safe 0 -loglevel panic -i "list.txt" -y -c:a copy -vn "%~nx1.mp3"
del /q "list.txt"
echo "success"
popd
goto :EOF
将版本放置在主文件夹中,然后双击:
我添加了一些内容来检测每个文件夹中是否已经存在 mp3 文件,如果是,则不会重新创建它,从而节省时间...
@echo off
setlocal EnableDelayedExpansion
chcp 65001 >nul
ffmpeg -version 1>nul 2>nul
if errorlevel 1 (
echo "ffmpeg not found"
@pause
exit
)
for /f "delims=" %%a in ('dir /b /ad *.*') do call :CreateMP3 "%%~dpnxa"
exit
:CreateMP3
cls
echo.
echo Processing Folder: %~nx1
pushd "%~1\s"
if exist "list.txt" del /q "lista.txt"
for /f "delims=" %%a in ('dir /b "*.ts"') do echo file '%%~a'>>"list.txt"
if not exist "%~nx1.mp3" ffmpeg -f concat -safe 0 -loglevel panic -i "list.txt" -y -c:a copy -vn "%~nx1.mp3"
del /q "list.txt"
echo "success"
popd
goto :EOF
答案2
for /d /r %%i in (*)do pushd "%%~fi" && call mp3.bat & popd
您可以使用for /d /r
循环,它将遍历所有文件夹,并在循环中的每个文件夹中使用pushd
命令后跟call
运行你的mp3bat
在当前文件夹中,一旦完成该操作,运行popd
FOR /R - Loop through files (recursively) FOR /D - Loop through several folders/directories
The option /D /R is undocumented, but can be a useful combination, while it will recurse through all subfolders the wildcard will only match against Folder/Directory names (not filenames) Note: Source linked to ss64.com
其他资源: