john c. j.
john c. j.

Reputation: 1175

findstr: Gives more matches than expected

I don't understand why findstr doesn't work as I want.

I have the following files in my test directory:

aaa.jpg
bbb.png
ccc.svg

aaa_s.jpg
bbb_s.png
ccc_s.svg

aaa_small.jpg
bbb_small.png
ccc_small.svg

And I have the following line to pass directly to cmd.exe:

for /f "delims=" %f in ('dir /b /a:-d ^| findstr /ile "gif jpg png svg" ^| findstr /ie "_s.*"') do echo "%f"

To my opinion, it should match the following files:

aaa_s.jpg
bbb_s.png
ccc_s.svg

However, it's actually matches

aaa_s.jpg
bbb_s.png
ccc_s.svg

aaa_small.jpg
bbb_small.png
ccc_small.svg

What I'm doing wrong?

Upvotes: 0

Views: 170

Answers (1)

Compo
Compo

Reputation: 38579

. is a FindStr wildcard for any character, and * is for zero or more occurrences of the previous character. So obviously _s.* matches _s followed by any character zero or more times; which covers _sm.

Please open a Command Prompt window, type findstr /?, press the 'enter' key, and read the usage information.

BTW, what's wrong with using:

Dir /B /A:-D *_s.*

If needs be you could pipe that to FindStr with /I /L /E ".gif .jpg .png .svg" for example:

Dir /B /A:-D *_s.* | FindStr /I /L /E ".gif .jpg .png .svg"

Alternatively you could include multiple matches to your Dir command and forget about using FindStr entirely:

Dir /B /A:-D "*_s.gif" "*_s.jpg" "*_s.png" "*_s.svg"

Upvotes: 1

Related Questions