arielbeje
arielbeje

Reputation: 25

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

I want to convert .flac files from X:\Music\flac\flacfolder\name.flac to X:\Music\mp3\flacfolder\name.mp3 using ffmpeg, but I couldn't find how to loop through while passing the directory to a different command and manipulating it.

Upvotes: 0

Views: 1495

Answers (3)

aschipfl
aschipfl

Reputation: 34899

Usually, to process files recursively, I would suggest to use the for /R loop. 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; then I wrap around a for /F loop to read the list of relative paths and to resolve them related to the target folder; 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):

@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:

@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

Upvotes: 3

Eldad Assis
Eldad Assis

Reputation: 11045

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

Upvotes: 1

Simon Catlin
Simon Catlin

Reputation: 2229

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

Upvotes: 1

Related Questions