Search code examples
batch-filemp4avihandbrake

Batch File Loop Skip File if name contains


I am creating this batch file, that works with handbrakecli, to batch convert avi to mp4.

However I am stuck in how to continue the loop and skip the current file inside a loop.

FOR /R "%somepath%" %%G in (*.avi) DO (

rem skip if filename contains word trailer

rem skip if file name contains word sample

rem do conversion
)

This currently doesn't work in skipping the files that contain trailer or sample

I have tried using find or findstr and both fail to skip.

    echo "%%G" | c:\windows\system32\findstr /i "trailer" > NUL
    If %ERRORLEVEL% EQU 1 set skip Yes

Here is for sample.

    echo "%%G" | c:\windows\system32\findstr /i "sample" > NUL
    If %ERRORLEVEL% EQU 1 set skip Yes

If a file contains either trailer or sample, I do not want to do any handbrakecli conversions, but to just skip it.

I do echo's to display which files get converted, and it does include files with Sample or sample in the name.

I have tried using find or findstr and both fail to set skip to yes

if skip == No do ( rem do conversion )

I only want to convert non-trailer/sample avi files.

Thank you for your time.


Solution

  • try this, put your conversion commands in the loop and remove the word echo before handbrakecli if the output is OK:

    @echo off &setlocal
    FOR /R "%somepath%" %%G in (*.avi) DO (
        set "fpath=%%G"
        set "fname=%%~nG"
        setlocal enabledelayedexpansion
        if "!fname!"=="!fname:trailer=!" if "!fname!"=="!fname:sample=!" (
            echo handbrakecli.exe "!fpath!" &rem put your conversion  command here
            >>"logfile.log" echo !fname!
        )
        endlocal
    )
    

    The file name+file path is in the variable "!fpath!".

    Added some code concerning the needs of the OP:

    @echo off &setlocal
    rem replace avi with mp4 files in my movie folder
    rem grab 4 random folders with avi in them and no mp4
    
    rem Settings for this Batch File
    set "moviepath=H:\Movies"
    set "logfile=C:\Documents and Settings\%USERNAME%\LogFiles\avi_converter.log"
    
    rem check if log file exists
    if not exist "%logfile%" echo(>"%logfile%"
    
    rem create empty convert file
    copy nul "convert_movies.bat" >nul 2>&1
    
    rem add echo off
    echo @echo off >>"convert_movies.bat"
    
    rem set counter
    SET /A COUNT=1
    
    FOR /R "%moviepath%" %%G in (*.avi) DO (
        set "fpath=%%~fG"
        set "fname=%%~nG"
        setlocal enabledelayedexpansion
    
        rem check if count greater than 4
        if !COUNT! gtr 4 goto:eof
    
        if "!fname!"=="!fname:trailer=!" if "!fname!"=="!fname:sample=!" (
            rem echo handbrakecli.exe "!fpath!" &rem put your conversion  command here
    
                rem Send File To HandBrakeCLI
                CALL :DOHandBrakeCLI "!fpath!"
    
                rem Delete File
                CALL :DeleteOldFile "!fpath!"
    
                rem Add Log Entry
                CALL :LogEntry "!fpath!"
    
                rem add line break space
                echo( >>"convert_movies.bat"
    
                endlocal
                rem increment counter
                SET /A COUNT+=1
    
        ) else endlocal  
    )
    rem end main program, to close cmd window replace it with EXIT
    goto:eof
    
    :DOHandBrakeCLI
    rem skip if the parameter is empty
    IF "%~1"=="" goto:eof
    For %%A in ("%~1") do (
        Set "Folder=%%~dpA"
        Set  "Name=%%~nxA"
    )
    rem echo %Folder%%Name%
    echo start /b "" "c:\handbrakecli\HandBrakeCLI.exe" -i "%~1" -o "%Folder%%~n1.mp4" --preset="High Profile">>"convert_movies.bat"
    exit /b
    
    :DeleteOldFile
    rem skip if the parameter is empty
    IF "%~1"=="" goto:eof
    For %%A in ("%~1") do (
        Set "Folder=%%~dpA"
        Set "Name=%%~nxA"
    )
    rem sends parameters to deletefile which will make sure new file exists before deleting old one
    echo c:\projects\deletefile.bat "%~1" "%Folder%%~n1.mp4">>"convert_movies.bat"
    exit /b
    
    :LogEntry
    rem skip if the parameter is empty
    IF "%~1"=="" goto:eof
    echo "%~1">>"%logfile%"
    exit /b