简体   繁体   中英

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

I am trying to do essentially the same as in this question , ie 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, ie I get

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM