Equivalent of a wildcard in find for batch?

1

I'm almost certain this is a duplicate but I was unable to find it after a lot of searching so forgive me.
I'm looking for a way to have the equivalent of a wildcard in the find command. For instance, If I want to find a line in a file that has 09/(some unknown string here)/2018 How would I go about doing this? I'm open to using the findstr command too.

Mark Deven

Posted 2018-09-03T16:23:48.843

Reputation: 1 101

Have you tried actual wildcards (% *)? – Máté Juhász – 2018-09-03T16:30:47.210

Yes it just searched for them. I hadnt tried with findstr though and it needs a period too – Mark Deven – 2018-09-03T16:46:07.473

Answers

2

findstr supports some simple regular expressions - see the bottom of the output of findstr /?. To match any run of characters, use .*. The dot matches any single character, and the asterisk allows matching the previous character (any character, in this case) any number of times. So this will find any lines that contain 09/ and /2018 with any characters in between:

findstr "09/.*/2018" MYFILE.TXT

If you only want to match lines where the unknown string is exactly two characters long (since your search string appears to be a date), just use two single-character wildcards:

findstr "09/../2018" MYFILE.TXT

Ben N

Posted 2018-09-03T16:23:48.843

Reputation: 32 973

1For what it's worth, I searched Super User before posting this answer and was also unable to find a straight answer for you. – Ben N – 2018-09-03T16:39:46.753

This isnt working actually, it seems it works without the .* example: findstr "09/ /2018"... I dont understand findstr at all :D – Mark Deven – 2018-09-03T19:06:34.057

@MarkDodsons That's interesting; the .* method seems to be working for me. May I see an example of a line that isn't found when it should be? – Ben N – 2018-09-03T19:13:32.210

I figured it out, it does not work with /C: that was my problem – Mark Deven – 2018-09-04T01:19:16.053

@MarkDodsons - It does work with /c:regexSearch if you also use the /r option. /c: defaults to literal search, but can be overridden with /r. – dbenham – 2018-09-04T22:38:26.657