Home > Back-end >  How to make a batch file loop resume from where it was by looping through the files listed on a .txt
How to make a batch file loop resume from where it was by looping through the files listed on a .txt

Time:02-17

I want to start down mixing hundreds of surround media files in a folder to stereo, but this is a process that will take a lot of time. I'd like to create a batch file that executes my ffmpeg command to this set of files (probably listed in a .txt with dir /s /b) that I can run whenever my PC is on, but also keeps a record of already processed files to be excluded on the next run.

I know I can easily keep track of already processed files by simply adding something like if errorlevel 0 echo "%%~fg">>processed.txt to my loop, but I'm finding it challenging to come up with a way to ignore these files when running the script the next time.

Of course I could always manually edit the file list to be looped and remove the ones already processed, but I wonder if there is a clever way to do it programatically

CodePudding user response:

An example of using a log with findstr. replace the definition of Set "SourceList=%~dp0list.txt" with the filepath of the file used to store the list of files for processing, or modify the for /f loop options to iterate over the output of your Dir command.

@Echo off

 If not exist "%TEMP%\%~n0.log" break >"%TEMP%\%~n0.log"

 Set "SourceList=%~dp0list.txt"
 For /f "usebackq delims=" %%G in ("%sourceList%")Do (
    %SystemRoot%\System32\Findstr.exe /xlc:"%%G" "%TEMP%\%~n0.log" >nul && (
        Rem item already processed and appended to log. do nothing.
    ) || (
        Rem item does not exist in log. Proccess and append to log.
        Echo(Proccess %%G
        >>"%TEMP%\%~n0.log" Echo(%%G
    )
 )
  • Related