olmanRvr Posted November 10, 2016 Share Posted November 10, 2016 (code given below) I m trying to compare the physical hard drive number returned by a autoit function to the same number hard coded in to the script by using stringcompare and also “==” operator.I expected the result of the comparison would produce a “match” but stringcompares returns non-zero(not equal).However consolewrite outputs the same values.Cant figure out what the mistake is. Will greatly appreciate any help Thanks olmar expandcollapse popup#include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <Date.au3> #include <GUIConstantsEx.au3> #include <GuiEdit.au3> #include <MsgBoxConstants.au3> #include <StringConstants.au3> chkCond() Func chkCond() Local $constSr="2020202020202020202059205337544c41335452" Local $getSr=String(gtSrNo()) Local $ans $ans= StringCompare($constSr, $getSr) MsgBox(0,0,"const str is:"&$constSr&@CRLF&"machi str is:"&$getSr) MsgBox(0,0,"Result is: "&$ans&@CRLF) ConsoleWrite("The machine serial is:--------- "&$getSr&@CRLF) ConsoleWrite("The CONST serial is:--------- "&$constSr&@CRLF) If $constSr==$getSr Then MsgBox(0,0,"Same") Else MsgBox(0,0,"Not Same") EndIf EndFunc Func gtSrNo();returns physical hard disc serial num $wbemFlagReturnImmediately = 0x10 $wbemFlagForwardOnly = 0x20 $colItems = "" $strComputer = "localhost" $Output="" $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2") $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", _ $wbemFlagReturnImmediately + $wbemFlagForwardOnly) If IsObj($colItems) then For $oItm In $colItems $strPowerManagementCapabilities = $oItm.PowerManagementCapabilities(0) $Output = $Output & $oItm.SerialNumber & @CRLF Next Else Msgbox(0,"WMI Output","Pl No WMI Objects Found for class: " & "Win32_DiskDrive" ) Endif Return $Output EndFunc Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 10, 2016 Moderators Share Posted November 10, 2016 olmanRvr, Try comparing the 2 strings converted to binary with StringToBinary and see if they match - there are often non-printing characters which mess up normal string comparisons. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
czardas Posted November 10, 2016 Share Posted November 10, 2016 (edited) It could be due to a mismatch in case sense. The two comparisons are different because the comparison operator "==" performs a case sensitive comparison in AutoIt. Try a single equals sign and see if that produces the result you expect. Or set StringCompare() to perform a case sensitive comparison. Edited November 10, 2016 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
olmanRvr Posted November 11, 2016 Author Share Posted November 11, 2016 13 hours ago, Melba23 said: olmanRvr, Try comparing the 2 strings converted to binary with StringToBinary and see if they match - there are often non-printing characters which mess up normal string comparisons. M23 Hi, As per your suggestion changed the values to binaries:- $constSr=StringToBinary($constSr) $getSr=StringToBinary($getSr) But again the strings don't match.Also tried czardas's suggestion for using "=" instead of "==" to compare the strings.No success.I noticed if the constant $constSr is assigned the value from a function call it works(code below).But for my purpose I need to hard code it.Any suggestion please. thanks expandcollapse popupchkCond() Func chkCond() Local Const $constSr=gtSrNo2();assigns value instead of hard coding Local $getSr=gtSrNo() Local $ans $ans= StringCompare($constSr, $getSr) If $ans==0 Then MsgBox(0,0,"same") Else MsgBox(0,0,"different") EndIf ConsoleWrite("The machine serial is:--------- "&$getSr&@CRLF) ConsoleWrite("The CONST serial is:--------- "&$constSr&@CRLF) If $constSr==$getSr Then MsgBox(0,0,"Same") Else MsgBox(0,0,"Not Same") EndIf EndFunc Func gtSrNo();returns physical hard disc serial num $wbemFlagReturnImmediately = 0x10 $wbemFlagForwardOnly = 0x20 $colItems = "" $strComputer = "localhost" $Output="" $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2") $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", _ $wbemFlagReturnImmediately + $wbemFlagForwardOnly) If IsObj($colItems) then For $oItm In $colItems $strPowerManagementCapabilities = $oItm.PowerManagementCapabilities(0) $Output = $Output & $oItm.SerialNumber & @CRLF Next Else Msgbox(0,"WMI Output","Pl No WMI Objects Found for class: " & "Win32_DiskDrive" ) Endif Return $Output EndFunc ;Func gtSrNo2() is exact copy of "Func gtSrNo()" .Copy paste Func gtSrNo() below Link to comment Share on other sites More sharing options...
czardas Posted November 11, 2016 Share Posted November 11, 2016 (edited) Try removing the longer string from the shorter string and see if you are left with anything: perhaps CRLF. If the strings are the same length, then try the comparing the string one character at a time until you hit a mismatch. Examine the binary of each string. Post a working reproducer - i.e. all the code. Edited November 11, 2016 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
czardas Posted November 11, 2016 Share Posted November 11, 2016 (edited) Change this line and it works when compared to the (non-binary) hard coded string value: Local $getSr= StringStripWS(gtSrNo(), 8) Match The machine serial is:--------- WD-WCC3F4TKK622 The CONST serial is:--------- WD-WCC3F4TKK622 Edited November 11, 2016 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
olmanRvr Posted November 11, 2016 Author Share Posted November 11, 2016 17 minutes ago, czardas said: Change this line and it works when compared to the hard coded version (non-binary): Local $getSr= StringStripWS(gtSrNo(), 8) Hi czardas, Absolutely great!!! It is working!! This is what i wanted.Issue SOLVED Changed assignment of $getStr FROM $getStr=getStr() TO $getSr= StringStripWS(gtSrNo(), 8) Thanks a whole lot olmar czardas 1 Link to comment Share on other sites More sharing options...
czardas Posted November 11, 2016 Share Posted November 11, 2016 (edited) You are welcome. I guess it's fine as it is, although might be better to only strip leading and trailing white spaces (1+2). I just put the value 8 to strip all spaces as a quick test. Take a look at StringStripWS() parameter options in the help file. It's good to know for the future anyway! ... and please mark the topic solved. Edited November 11, 2016 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now