Page 1 of 1 1
Topic Options
#114326 - 2004-02-18 05:06 PM Reading registry value strings
operez Offline
Getting the hang of it

Registered: 2003-08-08
Posts: 75
I need to read the value of a registry key and compare the value's string to a list of acceptable strings. If the string exists, I need to make a note that it already exists so that when I read the next value I know if I have a duplicate string. If the string doesn’t exist, I need to create it and make a note that the value now exists. And, if I run into a duplicate string, I need to delete that value.

Could someone point me in the right direction? I can’t seem to get the logic started. Should I create a multi element array with the first element holding the acceptable string and the second element holding a value indicating whether the string already exist or not? Should I scrap the array concept and use some other method? Can someone help me out?

Top
#114327 - 2004-02-18 05:39 PM Re: Reading registry value strings
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Code:

$a=readvalue('hkcu\software\kixtart','value')
$b='aaa','bbb','ccc'
select
case @ERROR
; value does not exist
$rc=writevalue('hkcu\software\kixtart','value','1234','REG_SZ')
case ascan($b,$a)
; value in array
case 1
; value not in array
endif

_________________________
There are two types of vessels, submarines and targets.

Top
#114328 - 2004-02-18 05:50 PM Re: Reading registry value strings
operez Offline
Getting the hang of it

Registered: 2003-08-08
Posts: 75
Thanks, this gives me something to start with.
Top
#114329 - 2004-02-18 06:26 PM Re: Reading registry value strings
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
What if the string is not in the acceptable list?
Top
#114330 - 2004-02-18 06:31 PM Re: Reading registry value strings
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
That's what the CASE 1 is for.
_________________________
There are two types of vessels, submarines and targets.

Top
#114331 - 2004-02-18 06:38 PM Re: Reading registry value strings
operez Offline
Getting the hang of it

Registered: 2003-08-08
Posts: 75
OK, this will prove that I’m just a beginner. What my intent was to verify that a user has the three string values listed in my script. (And, now I realize I also need to check for the string value of OPEN)

So, I wrote this script and what is happening is that a user’s registry ends up with multiple values (OPEN10 … 20 … 30 and more) of the same string. I need to check the string value of all OPENx values and compare it to the list of three acceptable strings. If the string does not match I need to move on to the next value. If it’s a duplicate, I need to delete it. If it doesn’t exist I need to create it. Also, if I delete a value (example, OPEN3) because it is a duplicate I need to start renaming the next OPENx value. Boy, this gets complicated.


Code:

$Index = 0
$ValueIndex = 1
$MyAppValue = 1
:Loop1
$ValueName = ENUMVALUE ("HKEY_CURRENT_USER\Software\Microsoft\Office\8.0\Excel\microsoft excel", $Index)
If @ERROR = 0
If $ValueName = "OPEN" + $ValueIndex
If @ERROR = 0
$RegValue = ReadValue ("HKEY_CURRENT_USER\Software\Microsoft\Office\8.0\Excel\microsoft excel", $ValueName)
Select
Case $MyAppValue = 1
$OpenValue = "/R C:\Program Files\MyApp\XLFILES\BPSXL32.XLL"
Case $MyAppValue = 2
$OpenValue = "/R C:\Program Files\MyApp\XLFILES\BH_EXT.XLA"
Case $MyAppValue = 3
$OpenValue = "/R C:\Program Files\MyApp\XLFILES\BPSMA32.XLL"
EndSelect
If $RegValue = $OpenValue
$MyAppValue = $MyAppValue + 1
EndIf
EndIf
$ValueIndex = $ValueIndex + 1
EndIf
$Index = $Index + 1
goto Loop1
Endif
If $MyAppValue = 1 And $ValueIndex = 1
$ValueIndex = 1
Else
If $MyAppValue > 1
Goto EndFS
EndIf
EndIf
writevalue("Software\Microsoft\Office\8.0\Excel\microsoft excel", "OPEN" + $ValueIndex, "/R C:\Program Files\MyApp\XLFILES\BPSXL32.XLL", "REG_SZ")
$ValueIndex = $ValueIndex + 1
writevalue("Software\Microsoft\Office\8.0\Excel\microsoft excel", "OPEN" + $ValueIndex, "/R C:\Program Files\MyApp\XLFILES\BH_EXT.XLA", "REG_SZ")
$ValueIndex = $ValueIndex + 1
writevalue("Software\Microsoft\Office\8.0\Excel\microsoft excel", "OPEN" + $ValueIndex, "/R C:\Program Files\MyApp\XLFILES\BPSMA32.XLL", "REG_SZ")
:EndFS




I hope I am making sense.

Top
#114332 - 2004-02-18 06:57 PM Re: Reading registry value strings
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

That's what the CASE 1 is for.




No, I meant what did operez want to do if it wasn't in the list

This is what I came up with:

; Create "acceptable" array
$asAcceptable="/R C:\Program Files\MyApp\XLFILES\BPSMA32.XLL"
,"/R C:\Program Files\MyApp\XLFILES\BH_EXT.XLA"
,"/R C:\Program Files\MyApp\XLFILES\BPSMA32.XLL"

; Parallel "present" array of same size
Dim $aiPresent[Ubound($asAcceptable)]

$sKeyName="HKEY_CURRENT_USER\Software\Microsoft\Office\8.0\Excel\microsoft excel"

$iValueIndex=0
$sValueName=EnumValue($sKeyName,$iValueIndex)
While @ERROR=0
$sKeyValue=ReadValue($sKeyName,$sValueName)
$iIndex=ascan($asAcceptable,$sKeyValue)
If (1+$iIndex)
; Content of value is in acceptable list
$iIndex=ascan($asAcceptable,$sKeyValue)
If $aiPresent[$iIndex]
"Duplicate entry '"+$sKeyValue+"' found at "+$sKeyName+", "+$sValueName+@CRLF
$rc=DelValue($sKeyName,$sValueName)
; Deleted value, so must back up index for enum.
$iValueIndex=$iValueIndex-1
Else
$aiPresent[$iIndex]=1
EndIf
Else
; Not in acceptable array - ignored.
EndIf
$iValueIndex=$iValueIndex+1
$sValueName=EnumValue($sKeyName,$iValueIndex)
Loop

; Write missing keys
For $iIndex = 0 To Ubound($asAcceptable)
If Not $aiPresent[$iIndex]
"Writing '"+$asAcceptable[$iIndex]+"' to 'OPEN"+($iIndex+1)+"'" ?
$rc=WriteValue($sKeyName,"OPEN"+($iIndex+1),$asAcceptable[$iIndex],'REG_SZ')
EndIf
Next



Top
#114333 - 2004-02-18 07:02 PM Re: Reading registry value strings
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
operez:

See the UDF Forum for helpful UDFs, e.g. ArrayenumKey and ArrayEnumValue
Code:

$values=arrayenumvalue('HKCU\Software\Microsoft\Office\8.0\Excel\microsoft excel')
for each $value in $values
next



Would it not be easier to just write the first three registry value with the three approved files, and delete all other values independent of what they contain?
_________________________
There are two types of vessels, submarines and targets.

Top
#114334 - 2004-02-18 09:02 PM Re: Reading registry value strings
operez Offline
Getting the hang of it

Registered: 2003-08-08
Posts: 75
That was what I originally did and later found out that other programs had their own Add-in macros and my script was deleting them. That’s why this thing got so complicated.

Thanks for everyone’s help.

Top
#114335 - 2004-02-19 02:32 AM Re: Reading registry value strings
operez Offline
Getting the hang of it

Registered: 2003-08-08
Posts: 75
Richard, I have tried your script and I am getting the following error:

Script error: expected expression!
$iIndex=ascan($asAcceptable,$sKeyValue)

Top
#114336 - 2004-02-19 02:48 AM Re: Reading registry value strings
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
What's your KiXtart version? Generally, code posted is asumed to run under the latest Kixtart release, currently KiXtart v4.22

Pleas read ABC's of KiXtart board etiquette and message to new forum users , especially Section F, to prevent further confusion.
_________________________
There are two types of vessels, submarines and targets.

Top
#114337 - 2004-02-19 10:18 AM Re: Reading registry value strings
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

Richard, I have tried your script and I am getting the following error:

Script error: expected expression!
$iIndex=ascan($asAcceptable,$sKeyValue)




Indeedy, as Jens has said, you need to ensure that you are up to date. There are array scanning UDFs which you can use to replace the functionality of ASCAN on earlier versions of KiXtart, but it is far easier to update and get access to all the new features.

BTW, the second "ascan()" is redundant and can be removed - a little over-eager cutting'n'pasting.

BTW2, I've not tested the script as I don't have the environment, but it should be syntactically correct and just require a little localised tweaking.

Top
#114338 - 2004-02-19 04:18 PM Re: Reading registry value strings
operez Offline
Getting the hang of it

Registered: 2003-08-08
Posts: 75
Now I know why you have an icon that looks like its embarrassed.

Sorry, I was running v4.02 although some how I thought I was on v4.12. I have downloaded v4.22 and will be giving it a shot sometime today.

Thanks for everyone’s help.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 978 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.068 seconds in which 0.028 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org