No KiXtart.org Moderators or Administrators aside from myself are allowed to post in this thread.
(This is for those with less experience to learn, not for bored, experienced users to bump their post count)
(If you're able to easily do this task then please don't participate with any solutions)
(If the task is too easy then I'll increase the difficulty level next time)
No using IM,PM or Google to locate/find an answer (you're on the honor system here).
Objective: Check if registry key contains an expected value of "Enterprise Terminal Server"
Using REGEDIT.EXE create the key Golfing under HKEY_CURRENT_USER\Software Then under the new Golfing key create a new Multi-String Value named Round1 and enter the following value. Enterprise {press enter key} Terminal {press enter key} Server {press enter key}
Now using KiXtart check if that value exists or not.
DO NOT Post any solution for this though until Friday, Sep 22
Just a script method is fine. Does not need to be a UDF.
Want to see if the user knows how to do it.
I agree with Gargoyle that it might be nice to at least briefly explain what your code is doing or what you "think" your code is doing so that if needed it can be critiqued.
I'm in no way wanting to slam anyone for their code or explanation. Just want to help others to get better at scripting overall.
;Read the value at the given location $R = ReadValue("HKCU\Software\Golfing","Round1")
;Split the value as a Reg_Multi will include the | character $S = Split($R,"|")
;Now take each of the elements and put them back together ;with a space to check the value = what we want $J = Join($S," ")
;Does it match? If $J = "Enterprise Terminal Server ";space added at end as the join adds one ? "We have a match. The string returned was: " $J Else ? "No match found. The string returned was: " $J EndIf
;The value of $J was verified by printing out each of the elements of ;$S Array with the following code
/*
For $ = 0 to Ubound($S) ? $S[$] Next
*/
;When attempting to do the join, with the optional "count", it did not work. ;I was unable to determine why the following would not work
/* $J = Join($S," ",Ubound($s)-1) */
;To make this more robust one should verify that the value exist first ;then they should read the value type and write a routine to know how the ;expected return is going to be formatted, by no means should the following ;snippet be deemed fully functional, one would need to evaluate the returns ;of the different data types and build the appropriate "CASE" statement for ;that scenario
;Example
/*
If KeyExist("HKCU\Software\Golfing\Round1") = 1 $Type = Readtype("HKCU\Software\Golfing","Round1") $R = ReadValue("HKCU\Software\Golfing","Round1")
Select Case $Type = "Reg_Multi_SZ" $S = Split($R,"|") $J = Join($S," ") Case $Type = "Reg_SZ" $J = $S Case 1 @Error = 5 EndSelect
$ = Split(ReadValue('HKCU\Software\Golfing', 'Round1'), '|') For Each $a in $ If InStr('Enterprise Terminal Server', $a) = 0 Exit EndIf Next ? 'Value Present'
Thanks for the solutions Paul, both work as expected.
Though upon reviewing your code I'm not so sure I'd place you in the noobie category. That's okay though as we progress the task will become more difficult.
I'll post another solution once Bjorn is done unless someone else posts similar code.
$key='HKEY_CURRENT_USER\Software\','Golfing','Round1','Enterprise Terminal Server' ;The first two is the key, third is the value, and forth the data we are looking for.
$wr=FindMe($key) ; will return a code to @error: 0 for true, and 1 for not true @error
Function FindMe($key) Dim $ch,$found,$index,$valuename,$KeyF,$JoinKey,$Valuename2,$wr,$split,$inst $ch=KeyExist($key[0]) ; Does the HKEY_CURRENT_USER_\Software\ exist? if $ch ;If it does.... $found=0 $index=0 ;Setting values so the while-statement will not run forever..
while @error=0 and $found=0 ;While no errors and $found is still 0 $ValueName = ENUMKEY($key[0],$index) ;Lists the subkey, $index points to the 'number' in the list if $ValueName = $Key[1] $found=1 ;If the subkey is the same as the $Key[1] (second val in array), $found is set to 1 $KeyF=$key[0],$ValueName ;make an array out of $Key[0] and $ValueName $JoinKey=Join($KeyF,'') ;Join 'em together $wr=ReadValue($JoinKey,$Key[2]) ;Reads the data of value in the key $split=Split($wr,'|') ;Current format is seperated by '|', therefore remove those $wr=Join($split,' ') ; and join 'em together $inst=Instr($wr,$Key[3]) ;Does $wr contain the same as $Key[3] ? if $inst exit 0 else exit 1 endif ; If it does, exit with a 0, otherwise exit with a 1 else $Index = $Index + 1 ;$ValueName wasn't the same as $Key[1], continues to look for matches. endif loop
exit @error ; In case of an unexpected error comes along, just exit with it (dunno if really needed)
My second piece was not meant to be functional and was never tested. However at your request...
Code:
Dim $Type, $S, $J, $R
If KeyExist("HKCU\Software\Golfing") = 1 $Type = Readtype("HKCU\Software\Golfing","Round1") $R = ReadValue("HKCU\Software\Golfing","Round1")
Select Case $Type = "Reg_Multi_SZ" $S = Split($R,"|") $J = Join($S," ") Case $Type = "Reg_SZ" $J = $R Case 1 @Error = 5 EndSelect If $J = "Enterprise Terminal Server ";space added at end as the join adds one ? "We have a match. The string returned was: " $J Else ? "No match found. The string returned was: " $J EndIf
Well - My version looks like a novell, but being a function makes it a bit usefull.. BUT, not happy about how it goes thro the reg. Your versions doesn't lack error-checking tho - so I see no point in using a novell over a fewer lines of code...
REMINDER: These are My comments and they may not fully agree with other long term / old time scripters.
At this time if other Admins or Moderators or those with a good understanding of KiXtart want to comment please do, but don't post just to bump your post count.
And for those who want a Golfed down version here is an example.
Note though that for production scripts they should contain meaningful variable names and perform at least minimal error checking as well as some amount of documentation on what the script is doing.
Using the IIF statement works if you understand the returns to put in. Since you are allowed to put in anything you want, an explanation of why 0 and 1 are used are in order I believe.
The primary reason for bringing this up is that I would have used -1 and 0, and that comes from using KiXforms.
Well I'm sure there are times when -1 is appropriate but in this case we're simply looking for a flag to show that we did not find the value so anything aside from 0 would indicate it was not found.
Comments for your 2nd solution similar to first with regards to using var names with meaning if used in larger script, though not the case here.
Good idea for splitting based upon Reg Type
Here is a method to remove the space from your output check.
I learned something new! I am a util kix scripter and always trying to find better ways of getting the task done. I was going with that we were to find if 'Enterprise Terminal Server' was present.
Code:
DIM $Round1Value $Round1Value = Split(ReadValue('HKCU\Software\Golfing', 'Round1'), '|') ? IIF(Trim(Join($Round1Value, ' ',UBound($Round1Value))) = 'Enterprise Terminal Server', 'Value Present', 'Value Not Present')
I like Learning Series better than Mini Golf. Following the principles of best coding practice can only improve beginners and intermediate scripters. I never had used IIF or Join in my production environment, but you know for sure I will review some of my scripts to see if they can be improved. You can teach an old dog new tricks. Good stuff!
Thank you Doc for this - It's really been a learing experience! I'll try to follow your feedback as much as I can! But to get back to our discussion we had eariler regarding my array's, could you or someone else care to share the real way to handle 'em?
Quote: I like Learning Series better than Mini Golf. Following the principles of best coding practice can only improve beginners and intermediate scripters. I never had used IIF or Join in my production environment, but you know for sure I will review some of my scripts to see if they can be improved. You can teach an old dog new tricks. Good stuff!