简体   繁体   English

批处理:遍历文件夹和子文件夹并将其放入脚本

[英]Batch: Looping through folders and subfolders and get them into a script

我想使用ffmpeg.flac文件从X:\\Music\\flac\\flacfolder\\name.flacX:\\Music\\mp3\\flacfolder\\name.mp3 ,但在传递目录时找不到循环方法到另一个命令并进行操作。

Usually, to process files recursively, I would suggest to use the for /R loop . 通常,要递归处理文件,建议使用for /R循环 However, in this situation, since I guess you want to copy the directory hierarchy from the source to the target folder, I do not use it, because it resolves to absolute paths only. 但是,在这种情况下,由于我猜您想将目录层次结构从源复制到目标文件夹,因此我不使用它,因为它只能解析为绝对路径。 Instead I use xcopy /L , which does not copy anything (due to /L ), but lists all applicable items as paths relative to the source folder; 相反,我使用xcopy /L ,它不会复制任何内容(由于/L ),而是列出所有适用项作为相对于源文件夹的路径; then I wrap around a for /F loop to read the list of relative paths and to resolve them related to the target folder; 然后,我绕着for /F循环,以读取相对路径列表并解析与目标文件夹相关的路径; in the loop body finally, the ffmpeg needs to be placed (define the options to your needs and remove the preceding upper-case ECHO after having tested; the ffmpeg tool does not receive any relative paths but absolute ones only for both input and output files): 最后,将ffmpeg放置在循环主体中(根据需要定义选项,并在测试后删除前面的大写ECHOffmpeg工具不接收任何相对路径,而仅接收输入和输出文件的绝对路径) ):

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_SOURCE=X:\Music\flac\flacfolder" & rem // (absolute source path)
set "_TARGET=X:\Music\mp3\flacfolder"  & rem // (absolute target path)
set "_PATTERN=*.flac" & rem // (pure file pattern for input files)
set "_FILEEXT=.mp3"   & rem // (pure file extension of output files)

pushd "%_TARGET%" || exit /B 1
for /F "delims=" %%F in ('
    cd /D "%_SOURCE%" ^&^& ^(rem/ list but do not copy: ^
        ^& xcopy /L /S /Y /I ".\%_PATTERN%" "%_TARGET%" ^
        ^| find ".\" ^& rem/ remove summary line;
    ^)
') do (
    2> nul mkdir "%%~dpF."
    rem // Set up the correct `ffmpeg` command line here:
    ECHO ffmpeg -i "%_SOURCE%\%%~F" "%%~dpnF%_FILEEXT%"
)
popd

endlocal
exit /B

If you want the destination files in a flat folder structure instead, a for /R loop workes fine: 如果您希望目标文件使用平面文件夹结构,则for /R循环可以正常工作:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_SOURCE=X:\Music\flac\flacfolder" & rem // (absolute source path)
set "_TARGET=X:\Music\mp3\flacfolder"  & rem // (absolute target path)
set "_PATTERN=*.flac" & rem // (pure file pattern for input files)
set "_FILEEXT=.mp3"   & rem // (pure file extension of output files)

for /R "%_SOURCE%" %%F in ("%_PATTERN%") do (
    rem // Set up the correct `ffmpeg` command line here:
    ECHO ffmpeg -i "%_SOURCE%\%%~F" "%_TARGET%\%%~nF%_FILEEXT%"
)

endlocal
exit /B

I have an example I used in the past. 我有一个过去使用的示例。 I tried adding your structure to it. 我尝试将您的结构添加到其中。

@echo off
cd X:\Music\flac\flacfolder

for /F "tokens=1 delims=" %%i IN ('dir /s /b ^| findstr .flac') do (
  call :process_code "%%i"
)

goto end


:process_code
echo Running conversion for %1
:: Run your process here

goto :eof

:end

echo done!

I hope this helps 我希望这有帮助

Try something like: 尝试类似:

@echo off
setlocal
set FLAC_FOLDER=c:\temp\flacfolder
set MP3_ROOT_FOLDER=c:\temp\mp3folder
echo Processing folder [%FLAC_FOLDER%]...
for /f "tokens=*" %%F in ('dir "%FLAC_FOLDER%\*.flac" /a-d /b') do call :PROCESS_FLAC_FILE "%FLAC_FOLDER%" "%%F"
goto END

:PROCESS_FLAC_FILE
set PFF_FOLDER=%1
set PFF_FILE=%2
set PFF_FOLDER=%PFF_FOLDER:"=%
set PFF_FILE=%PFF_FILE:"=%
for /f %%I in ("%PFF_FILE%") do set PFF_MP3_FILE=%%~nI.mp3
echo Processing FLAC file [%PFF_FILE%] in folder [%PFF_FOLDER%]; output file is [%PFF_MP3_FILE%]...

REM Now call ffmpeg using the approriate variables.  Enclose the variables in double-quotes, e.g.:
REM (note, I don't know the syntax for ffmpeg, so I'm making this up as an example)

ffmpeg.exe -source "%PFF_FOLDER%\%PFF_FILE%" -target "%MP3_ROOT_FOLDER%\%PFF_MP3_FILE%"

goto END

:END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM