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.
This should work:
try this, put your conversion commands in the loop and remove the word
echo
before handbrakecli if the output is OK:The file name+file path is in the variable
"!fpath!"
.Added some code concerning the needs of the OP:
It's difficult to see where your post is pseudocode and where actual code.
The first sample contains only
REM
statements, so it's not surprising it apparently does nothing.Your second and third sample are effectively identical - the only difference is the target string. It's not surprising that the variable
skip
isn't set toYes
since the correct syntax isThe syntax you've posted will
REPORT
thatskip
is not defined - it ignores theYes
HOWEVER
this syntax is only usableOUTSIDE
of a"block statement"
- that is, a multiple-instruction statement (enclosed in parentheses) or cascaded&by&ersands. Batch firstPARSES
a complete statement - from theFOR
orif
through to the appropriate closing-parenthesis andTHEN
executes it. As part of thePARSING
phase, any%var%
- including%errorlevel%
is replaced by its value as it stands at the time the entire statement isparsed
- not as it changes due to the operation of thefor
.In order to use the value as it changes, you need to use
where
do_something
anddo_something_else)
may themselves be compound statements.OR
where the variable either is defined or not
OR
OR
But it's quite possible that
will give you an appropriate skeleton.
Echo the filename through
FINDSTR
and look for "sample" or "trailer"/i
case-insensitive. Findstr sets errorlevel 0 if either target string is found, 1 otherwise - and theif errorlevel x
syntax works on the dynamic value oferrorlevel
within a loop.The Batch file below solve the original question AND limit the number of converted files to a given number (that does not appear in the original question):