Batch File Loop Skip file if name contains

I create this batch file that works with handbrakecli to batch convert avi to mp4.

However, I am stuck on how to continue the loop and skip the current file inside the 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 does not currently work when skipping files containing a trailer or sample

I tried using find or findstr and both could not miss.

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

Here is an example.

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

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

I am doing an echo to show which files are being converted, and it includes files with a pattern or a pattern in the name.

I tried using find or findstr and both could not install skip yes

if skip == No (do the conversion)

I only want to convert avi files without a trailer / sample.

Thank you for your time.

+6
source share
5 answers

try this, put your conversion commands in a 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 code related to OP needs:

 @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 
+7
source

This should work:

 @echo off FOR /R "%somepath%" %%G in (*.avi) DO ( echo "%%~nG" |findstr /i "trailer sample">nul || ( rem do conversion ) ) 
+3
source

It is hard to see where your message is pseudo-code and where the valid code is.

The first sample contains only REM expressions, so it is not surprising that it does nothing.

Your second and third examples are virtually identical - the only difference is the target line. Unsurprisingly, the skip variable is not set to Yes , because the correct syntax is

 if %errorlevel% equ 0 set skip=Yes 

The syntax you posted will be REPORT that skip not defined - it ignores Yes

HOWEVER this syntax is used only by OUTSIDE for a "block statement" , that is, instructions with multiple instructions (enclosed in parentheses) or cascading & using ampersands. The package first PARSES complete statement - from FOR or if to the corresponding closing parenthesis and THEN executes it. As part of the PARSING phase PARSING any %var% β€” including %errorlevel% is replaced with its value, because it stands at a time when the entire parsed statement parsed not change, since it works with FOR .

To use a value when changing it, you need to use

 if errorlevel 1 (do_something) else (do_something_else) 

where do_something and do_something_else) themselves can be compound statements.

OR

 if defined variable (do_something) else (do_something_else) 

where the variable is either defined or not

OR

 setlocal enabledelayedexpansion .... if !errorlevel! equ x (do_something) else (do_something_else) 

OR

  if !var! neq something (do_something) else (do_something_else) 

But it is entirely possible that

 FOR /R "%somepath%" %%G in (*.avi) DO ( echo(%%G|findstr /i "sample trailer" >nul if errorlevel 1 echo %%G ) 

will provide you with the appropriate skeleton.

Cut the file name through FINDSTR and look for the " FINDSTR " or "trailer" /i case insensitive. Findstr sets errorlevel 0 if one target string is found, otherwise, and if errorlevel x syntax works with the dynamic value of errorlevel inside the loop.

+2
source
 @ECHO on &SETLOCAL REM This script was inspired by Endoro expanded script (https://stackoverflow.com/a/16891696/10572786). REM This batch script will recursively search for all .mp4 files that don't have (x265) in the file name. Any valid results will be encoded with x265 using FFmpeg. The original .mp4 file will remain unchanged in it original folder with the new x265 version. REM Example: %PATH%\A.mp4 > %PATH%\A(x265).mp4 REM If you don't have ffmpeg.exe on your PC you must download or build it with Microsoft Visual Studios. I recommend you download and run media autobuild suite on GitHub: (https://github.com/jb-alvarado/media- autobuild_suite). REM Once ffmpeg is compiled/downloaded make sure to set it folder path as an environmental variable in Windows before running the script. Change the script working directory to your .mp4 files root folder using the "cd" command. REM !!BEGIN SCRIPT!! cd %USERPROFILE%\OneDrive\Desktop\Vids\ REM Set mp4PATH to the root folder you wish to recursively search. SET "mp4PATH=%USERPROFILE%\OneDrive\Desktop\Vids\" REM Create empty convert file. COPY NUL "convert_movies.bat" >NUL 2>&1 REM Add ECHO off. ECHO @ECHO off >>"convert_movies.bat" REM Recursively search root folder. FOR /R "%mp4PATH%" %%G IN (*.mp4) DO ( SET "fpath=%%~fG" SET "fname=%%~nG" SETLOCAL enabledelayedexpansion REM Ignore all files that have "(x265)" in the file name. IF "!fname!"=="!fname:*(x265)=!" ( CALL :DO_FFmpeg_CLI "!fpath!" ECHO(>>"convert_movies.bat" ) ELSE ENDLOCAL ) ) GOTO:EOF REM CALL variables for use in FFmpeg command line. :DO_FFmpeg_CLI IF "%~1"=="" GOTO:EOF FOR %%A IN ("%~1") DO ( SET "Folder=%%~dpA" SET "Name=%%~nxA" ) REM Export info to "convert_movies.bat and run ffmpeg.exe command line in the cmd.exe window. ECHO ffmpeg -y -i "%~1" -c:v libx265 -preset slow -crf 18 -c:a aac "%Folder%%~n1(x265).mp4">>"convert_movies.bat" && ffmpeg | ffmpeg -y -i "%~1" -c:v libx265 -preset slow -crf 18 -c:a aac "%Folder%%~n1(x265).mp4" EXIT /B PAUSE 
+1
source

The batch file below solves the original question AND limits the number of converted files to the given number (which does not appear in the original question):

 @echo off setlocal EnableDelayedExpansion rem Insert in the next line the list of files to skip set skip=/trailer/sample/ set count=0 FOR /R "%somepath%" %%G in (*.avi) DO ( if /I "!skip:/%%~nG/=!" equ "%skip%" ( echo Current file name is not in skip variable echo Do conversion on: %%G set /A count+=1 if !count! equ 20 goto :endLoop ) ) :endLoop echo Converted files: %count% 
0
source

Source: https://habr.com/ru/post/1484126/


All Articles