3

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!

i3i5i7
  • 130
  • 1
  • 2
  • 8

1 Answers1

6

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.

Magoo
  • 77,302
  • 8
  • 62
  • 84