Looking for Select EndSelect workaround

Started by stanl, August 01, 2020, 09:34:24 AM

Previous topic - Next topic

stanl

The script below fails to find the latest .NET version installed using Select...EndSelect (my version release is 528372). I added an if statement just to check my version. I realize the code code be written with a series of if and statements, and I think at one time a way was posted to have the case statements returned as integer rather than boolean (where they all fail). 
Code (WINBATCH) Select


;Get Installed .NET Latest Version
value = RegQueryDword(@REGMACHINE, "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\[Release]",0)
Select value
   Case value >= 528040
   Message("Current .NET Version","4.8 or later")
   Break
   Case value >= 461808
   Message("Current .NET Version","4.7.2")
   Break
   Case value >= 461308
   Message("Current .NET Version","4.7.1")
   Break
   Case value >= 460798
   Message("Current .NET Version","4.7")
   Break
   Case value >= 394802
   Message("Current .NET Version","4.6.2")
   Break
   Case value >= 394254
   Message("Current .NET Version","4.6.1")
   Break
   Case value >= 393295
   Message("Current .NET Version","4.6")
   Break
   Case value >= 379893
   Message("Current .NET Version","4.5.2")
   Break
   Case value >= 378675
   Message("Current .NET Version","4.5.1")
   Break
   Case value >= 378389
   Message("Current .NET Version","4.5")
   Break
   Case value
   Message("Current .NET Version ":value, "Cannot Be Determined":@LF:"or Version not > 4.5")
   Break


EndSelect


if value >= 528040 Then Message("Current .NET Version ":value,"4.8 or later")


Exit

td

It is not clear to me what you mean by "to have the case statements returned as an integer rather than boolean (where they all fail)".  Case statements are never returned and they can contain any WIL expression that evaluates to an integer. But perhaps this is what you are referring to.

Code (winbatch) Select
value = RegQueryDword(@REGMACHINE, "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\[Release]",0)  +0
x = value >= 528040
Select 1
   Case value >= 528040
   Message("Current .NET Version","4.8 or later")
   Break
   Case value >= 461808
   Message("Current .NET Version","4.7.2")
   Break
   Case value >= 461308
   Message("Current .NET Version","4.7.1")
   Break
   Case value >= 460798
   Message("Current .NET Version","4.7")
   Break
   Case value >= 394802
   Message("Current .NET Version","4.6.2")
   Break
   Case value >= 394254
   Message("Current .NET Version","4.6.1")
   Break
   Case value >= 393295
   Message("Current .NET Version","4.6")
   Break
   Case value >= 379893
   Message("Current .NET Version","4.5.2")
   Break
   Case value >= 378675
   Message("Current .NET Version","4.5.1")
   Break
   Case value >= 378389
   Message("Current .NET Version","4.5")
   Break
   Case 1
   Message("Current .NET Version ":value, "Cannot Be Determined":@LF:"or Version not > 4.5")
   Break
EndSelect

if value >= 528040 Then Message("Current .NET Version ":value,"4.8 or later")

Exit
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Thanks. Hadn't worked with Select or Switch in a while. I used your code but started with lowest value [ x = value >= 378389 ] and took out the if. Also, not huge need to keep replicating the message(). It is good to keep one's skills sharp even at the risk of a smackdown :-[
Code (WINBATCH) Select


value = RegQueryDword(@REGMACHINE, "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\[Release]",0)  +0
x = value >= 378389
latest =""
Select x
   Case value >= 528040
   latest = "4.8 or later"
   Break
   Case value >= 461808
   latest = "4.7.2"
   Break
   Case value >= 461308
   latest = "4.7.1"
   Break
   Case value >= 460798
   latest = "4.7"
   Break
   Case value >= 394802
   latest = "4.6.2"
   Break
   Case value >= 394254
   latest = "4.6.1"
   Break
   Case value >= 393295
   latest = "4.6"
   Break
   Case value >= 379893
   latest = "4.5.2"
   Break
   Case value >= 378675
   latest = "4.5.1"
   Break
   Case value >= 378389
   latest = "4.5"
   Break
   Case x
   latest = "Cannot Be Determined"
   Break
EndSelect


Message("Current .NET Version ":value, latest)


Exit

stanl

Threw in a UDF for return type.
Code (WINBATCH) Select


;Winbatch 2020A - Searching .NET latest release
gosub udfs
IntControl(73,1,0,0,0)
eType = RegEntryType(@REGMACHINE, "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\[Release]",0) +0
Message("SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\[Release]",entryType(eType))
value = RegQueryDword(@REGMACHINE, "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\[Release]",0) +0
x = value >= 378389
latest =""
Select x
   Case value >= 528040
   latest = "4.8 or later"
   Break
   Case value >= 461808
   latest = "4.7.2"
   Break
   Case value >= 461308
   latest = "4.7.1"
   Break
   Case value >= 460798
   latest = "4.7"
   Break
   Case value >= 394802
   latest = "4.6.2"
   Break
   Case value >= 394254
   latest = "4.6.1"
   Break
   Case value >= 393295
   latest = "4.6"
   Break
   Case value >= 379893
   latest = "4.5.2"
   Break
   Case value >= 378675
   latest = "4.5.1"
   Break
   Case value >= 378389
   latest = "4.5"
   Break
   Case x
   latest = "Cannot Be Determined"
   Break
EndSelect


Message("Current .NET Version ":value, latest)


Exit


:WBERRORHANDLER
geterror()
Message("Error Encountered",errmsg)
Exit


:udfs
#DefineSubRoutine geterror()
   wberroradditionalinfo = wberrorarray[6]
   lasterr = wberrorarray[0]
   handlerline = wberrorarray[1]
   textstring = wberrorarray[5]
   linenumber = wberrorarray[8]
   errmsg = "Error: ":lasterr:@LF:textstring:@LF:"Line (":linenumber:")":@LF:wberroradditionalinfo
   Return(errmsg)
#EndSubRoutine


#DefineSubRoutine entryType(eType)
   msg=""
   Select eType
      Case 0
         msg = "REG_NONE":@LF:"No value type"
         Break
      Case 1
         msg = "REG_SZ":@LF:"An ANSI string"
         Break
      Case 2
         msg = "REG_EXPAND_SZ":@LF:"An ANSI string that contains unexpanded references":@LF:"to environment variables (for example, %PATH%)"
         Break
      Case 3
         msg = "REG_BINARY":@LF:"Free form binary"
         Break
      Case 4
         msg = "REG_DWORD":@LF:"32-bit number"
         Break
      Case 5
         msg = "REG_DWORD_BIG_ENDIAN":@LF:"32-bit number High byte first"
         Break
      Case 6
         msg = "REG_LINK":@LF:"Symbolic Link (Unicode)"
         Break
      Case 7
         msg = "REG_MULTI_SZ":@LF:"A delimited list of ANSI strings."
         Break
      Case 8
         msg = "REG_RESOURCE_LIST":@LF:"Resource list in the resource map"
         Break
      Case 9
         msg = "REG_FULL_RESOURCE_DESCRIPTOR":@LF:"Resource list in the hardware description"
         Break
      Case 10
         msg = "REG_RESOURCE_REQUIREMENTS_LIST":@LF:"Resource requirements"
         Break
      Case 11
         msg = "REG_QWORD":@LF:"64-bit number"
         Break
      Case eType
         msg = "Cannot Determine Registry Type"
   Endselect
   Return("Registry Type ":eType:@LF:msg)
#EndSubRoutine


Return



snowsnowsnow

You really need to stick with:

Switch 1

and not

Switch AnythingElse

Using AnythingElse just leads to heartache and pain.

The idiom is always:

Switch 1
case condition1
case condition2
etc
endswitch

BTW, Switch and Select are synonyms; I always use Switch.

stanl

Quote from: snowsnowsnow on August 04, 2020, 04:09:31 AM
You really need to stick with:
Switch 1
and not
Switch AnythingElse


Like I originally confessed, never really used either Select or Switch that much. My initial intent was to compare if..then statements against Switch or Select, then I like realized 'just a few milliseconds'. You are correct Switch 1 is basically 'look for True' and the wrong use of AnythingElse promotes failure.