i3i5i7
i3i5i7

Reputation: 130

Batch File Loop - Skip file, if file name contains string

I am trying to do essentially the same as in this question, i.e. I want to loop through files in a directory, but exclude files that have in their name a certain string (in my case ".new.". However, the problem is that I am using

setlocal DisableDelayedExpansion

because I want the batch also to work with file names that contain exclamation marks. I have thus tried to make the solution work using directly the loop variable %%x instead of a new variable, but that does not appear to work:

setlocal DisableDelayedExpansion
For %%x in (*.mkv *.mp4) do (
  If "%%x" == "%%x:.new." (
    Echo Skipped "%%x"
  ) Else (
    Echo Processing "%%x"
  )
)

String matching doesn't work, i.e. I get

Processing "file.mkv"    
Processing "file.new.mkv"

Any hint for how I could get this to work would be greatly appreciated; thanks!

Upvotes: 3

Views: 9597

Answers (1)

Magoo
Magoo

Reputation: 80033

Batch string-manipulation commands can't be applied directly to metavariables like %%x.

echo %%x|findstr /i /L ".new.">nul
if errorlevel 1 (
 echo process %%x
) else (
 echo skip %%x
)

should work for you, finding the string .new. /l literally, /i case-insensitive. set errorlevel to 0 if found, non-0 otherwise.

Upvotes: 7

Related Questions