Home > Software engineering >  Windows Batch file - matching files that contain a pattern but DO NOT end with that same pattern
Windows Batch file - matching files that contain a pattern but DO NOT end with that same pattern

Time:03-14

I'm trying to match files in a batch file, that contain the pattern .log. in the name but do not end in .log .

I have tried to match files with the following command: dir *.log.*

This matches all that contains .log even if it is at the end. I've also tried with the following pattern: *\.log\.* , but the compiler looks for a path, which is not intended.

I would appreciate some help, thank you in advance!

CodePudding user response:

You can combine a FOR loop with an IF statement

read HELP FOR and HELP IF

and then try something like this in the command prompt

@for %a in (*.log.*) do @if /i not "%~xa"==".log" @echo %a

and then add it to your batch file

for %%a in (*.log.*) do (
  if /i not "%%~xa"==".log" (
    echo %%a
  )
)
  • Related