Joao Ismael
Joao Ismael

Reputation: 177

Windows Findstr

I'm trying to find files in a folder with specific pattern like:

abcd201 abcd001 abcd004

The folder contains files named

abcd(3 numbers)

I'm trying to use the pattern:

abcd[0,2][0][1,4] but currently not working.

DIR /b C:\Folder\abcd"[0,2][0][1,4]".txt

Thanks!

Upvotes: 0

Views: 425

Answers (2)

Aacini
Aacini

Reputation: 67266

The regex of your example would also match abcd204 name. You may find these 4 files in a simpler way:

for %a in (0 2) do for %c in (1 4) do dir /B C:\Folder\abcd%a0%c.txt 2>NUL

This method is faster than findstr's one, especially if the number of files is large.

Upvotes: 0

MC ND
MC ND

Reputation: 70961

dir command does not support regular expressions. You need to filter the output with findstr

dir /b "c:\folder\abcd*.txt" | findstr /r /c:"^abcd[02]0[14]\.txt$"

That is, use dir command to obtain a first approximation of what you are searching and then filter the list (pipe the dir command to findstr) to obtain only the list of required files.

The regular expression (/r) in findstr means: filter the lines, starting at the start of the line (initial ^), followed by abcd, followed by any character in the set [02], followed by a 0, followed by any character in the set [14], followed by a dot (a single dot means any character, so, it needs to be escaped \.), followed by the string txt and the end of the line ($).

Maybe you will need to add a /i switch to findstr to indicate it must ignore case when matching.

Upvotes: 2

Related Questions