#1 03 May 2021 15:58

Simon_Weel
Member
Registered: 17 Aug 2020
Posts: 4

FINDSTR exact match

I need to compare version numbers against a predefined number. So I set version=12345. And then I use WMIC to get the version number of a .exe file, like wmic datafile where name='<path\program.exe' get version. Then I need to compare the version number of the file to the previous set variable and therefore, I append the WMIC command with | findstr /ci:%version%.
So the whole line reads wmic datafile where name='<path\program.exe' get version | findstr /ci:%version%
So far so good. But there's a snag. FINDSTR will, ofcourse, match 12345. But it will also match 1234
So I have to perform some sort of exact string search. Regular Expressions to the rescue. Thing is, you have to have some sort of mental state to get a grasp of regular expressions. IMO. Anyway, after doing some searching, I found the syntax to do exact string matching. And then the line looks like this: wmic datafile where name='<path\program.exe' get version | findstr/r "\<%version%\>"
The things to note are /R and the syntax "\<Text_To_Search\>"
Maybe something to add to the page FINDSTR as an example?

Offline

#2 03 May 2021 18:16

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,115
Website

Re: FINDSTR exact match

The downside of matching the start and end is that it would also match a string like 1234512345

To get an exact match you can do this, no regex required:

Echo 12345| findstr /X "12345"

Offline

Board footer

Powered by FluxBB