#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,118
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

#3 28 May 2021 19:33

Hackoo
Member
Registered: 05 Feb 2015
Posts: 21

Re: FINDSTR exact match

Hi cool
Here is another approch if you want to get version of any application and compare them using WMIC  wink

@echo off & color 0B
Title Get Version of any application using WMIC
Set "VersionToCompare=91.0.864.37"
Set "Application=C:\Program Files\Microsoft\Edge\Application\msedge.exe"

echo( & echo( Getting Version of "%Application%"
Call :GetVersion "%Application%" Version

cls
If Defined Version (
	echo( & echo( "%Application%" ==^> Version : "%Version%"
) else (
	echo( & echo( Version is not defined for "%Application%"
)

If ["%VersionToCompare%"] EQU ["%Version%"] (
	Color 0A
	echo( & echo( The Two Vesion are Equal to ["%VersionToCompare%"]
) else (
	color 0C
	echo( & echo( The Two versions are not equal ! ["%VersionToCompare%"] NOT EQUAL TO ["%Version%"]
)
Pause
Exit
::----------------------------------------------------------------------------------------
:GetVersion <App> <Version>
Rem The argument %~1 represent the full path of the application without the double quotes
Rem The argument %2 represent the variable to be set (in our case %2=Version)
Set "App=%~1"
Set "App=%App:\=\\%"
FOR /F "tokens=2 delims==" %%I IN (
	'wmic datafile where "name='%App%%'" get version /Value 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::----------------------------------------------------------------------------------------

Last edited by Hackoo (28 May 2021 19:34)

Offline

Board footer

Powered by FluxBB