#167418 - 2006-09-11 10:20 AM
Code optimisation
|
Pieman
Fresh Scripter
Registered: 2002-08-20
Posts: 29
|
I have been using Kix for some time, though I have not done a tremendous amount of coding with it, I thought that it was about time I started looking at optimising the code I was creating. Now being a greenhorn and certainly not a programmer/scripter I was wondering how best to do this. Should I be using an array, DO - UNTIL or WHILE - LOOP commands, or is there another method which is better suited to the task at hand.
Here is an example of the code at hand, all it does is check for the existence of some shortcuts within a folder on the client desktop & if they don't exist create them using the WSHshortcut UDF from the UDF's board which works perfectly fine. Code:
$dfl = "%userprofile%\desktop\Company Procedures Manuals" $tgl1 = "\\server_name\Procedures_Forms" $tgl2 = "\\server_name\public"
If Exist ("$dfl\Environmental Procedures") = 0 $=wshShortcut("$dfl\Environmental Procedures","$tgl1\Environmental Procedures\current") EndIf If Exist ("$dfl\Health & Safety Procedures") = 0 $=wshShortcut("$dfl\Health & Safety Procedures","$tgl1\Health & Safety Procedures") EndIf If Exist ("$dfl\Company Form Control I INDEX.xls") = 0 $=wshShortcut("$dfl\Company Form Control I INDEX.xls","$tgl1\Company Forms & Manuals\Company Form Control I Index.xls") EndIf If Exist ("$dfl\Quality Manual") = 0 $=wshShortcut("$dfl\Quality Manual","$tgl1\QA Procedures Manual\TS Quality Manual") EndIf If Exist ("$dfl\Quality Procedures") = 0 $=wshShortcut("$dfl\Quality Procedures","$tgl1\QA Procedures Manual\Procedures Manual") EndIf If Exist ("$dfl\Concession Log") = 0 $=wshShortcut("$dfl\Concession Log","$tgl2\Concession Log\Concession Log.xls") EndIf If Exist ("$dfl\SOP Register") = 0 $=wshShortcut("$dfl\SOP Register","$tgl2\SOP Folder\SOP Register.xls") EndIf
If InGroup("Quality","Quality Concerns_gbl","Quality RO") If Exist ("$dfl\Customer Concerns Register.lnk") = 0 ;File NOT found, creating shortcut $=wshShortcut("$dfl\Customer Concerns Register","\\eltnts04\Quality\Concerns\CUSTOMER CONCERNS") EndIf Else If Exist ("$dfl\Customer Concerns Register.lnk") = 1 ;User not member of group, delete File if exists DEL "$dfl\Customer Concerns Register.lnk" /c EndIf EndIf
If I need to create and use an array, how the hell do I create it and pass variables/commands into a circulary process until all the shortcuts are created if they don't exist
|
|
Top
|
|
|
|
#167419 - 2006-09-11 10:31 AM
Re: Code optimisation
|
Lonkero
KiX Master Guru
   
Registered: 2001-06-05
Posts: 22346
Loc: OK
|
creating an array is simple as: $array = element1,element2,element3
then simply do for each: for each $element in $array ? $element next
above, you could have array of shortcuts and another one of their targets. then remove all if-wshShortcut-endif pieces and add just single: Code:
for $index=0 to ubound($a_shortcuts) ;increase $index value until it reaches the limit of array elements If Exist ($a_shortcuts[$index]) = 0 $=wshShortcut($a_shortcuts[$index],$a_targets[$index]) EndIf next Code:
|
|
Top
|
|
|
|
#167420 - 2006-09-11 01:34 PM
Re: Code optimisation
|
Pieman
Fresh Scripter
Registered: 2002-08-20
Posts: 29
|
Ok, so I think I have done this correctly, but running the code generates an error: Expected ')'! for the line Code:
$wshshortcut($a_shortcuts[Index],$a_targets[$index])
here is the complete code so far Code:
$a_shortcuts = "%userprofile%\desktop\Company Procedures Manuals\Environmental Procedures","%userprofile%\desktop\Company Procedures Manuals\Health & Safety Procedures","%userprofile%\desktop\Company Procedures Manuals\Company Form Control I INDEX.xls","%userprofile%\desktop\Company Procedures Manuals\Quality Manual","%userprofile%\desktop\Company Procedures Manuals\Quality Procedures","%userprofile%\desktop\Company Procedures Manuals\Concession Log","%userprofile%\desktop\Company Procedures Manuals\SOP Register" $a_targets = "\\eltnts04\procedures_forms\Environmental Procedures\current","\\eltnts04\procedures_forms\Health & Safety Procedures","\\eltnts04\procedures_forms\Company Forms & Manuals\Company Form Control I Index.xls","\\eltnts04\procedures_forms\QA Procedures Manual\TS Quality Manual","\\eltnts04\procedures_forms\QA Procedures Manual\Procedures Manual","\\eltnts04\public\Concession Log\Concession Log.xls","\\eltnts04\public\SOP Folder\SOP Register.xls"
For Each $element In $a_shortcuts For $index=0 To UBound($a_shortcuts) ;increase $index value until it reaches the limit of array elements If Exist ($a_shortcuts[$index]) = 0 $wshshortcut($a_shortcuts[Index],$a_targets[$index]) EndIf Next
Any idea what I am doing wrong...? Remeber I am an idiot when it comes to this sort of thing so a nice easy to understand explaination would be great.
Thanks
|
|
Top
|
|
|
|
#167421 - 2006-09-11 01:48 PM
Re: Code optimisation
|
Witto
MM club member
   
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
Code:
$wshshortcut($a_shortcuts[$Index],$a_targets[$index])
|
|
Top
|
|
|
|
#167423 - 2006-09-11 02:21 PM
Re: Code optimisation
|
Björn
Korg Regular
   
Registered: 2005-12-07
Posts: 953
Loc: Stockholm, Sweden.
|
Code:
;your line: $wshshortcut($a_shortcuts[$Index],$a_targets[$index]) ;correct line: $=wshshortcut($a_shortcuts[$Index],$a_targets[$index])
Tho, usage of variables usually is used with a name to indicate usage, but since a lot of the aging ones here codes like gods, they like removing unessary letters 'n stuff =)
Code:
For Each $element In $a_shortcuts ;Why? For $index=0 To UBound($a_shortcuts) ;increase $index value until it reaches the limit of array elements If Exist ($a_shortcuts[$index]) = 0 $wshshortcut($a_shortcuts[Index],$a_targets[$index]) EndIf Next ;missing a next here...
I don't se the reason to loop-a-loop it? What am I missing?
Edited by Björn (2006-09-11 02:30 PM)
|
|
Top
|
|
|
|
#167428 - 2006-09-11 03:38 PM
Re: Code optimisation
|
Björn
Korg Regular
   
Registered: 2005-12-07
Posts: 953
Loc: Stockholm, Sweden.
|
Pieman, this is what Jooel was talking about - Code:
for $index=0 to ubound($a_shortcuts) ;increase $index value until it reaches the limit of array elements If Exist ($a_shortcuts[$index]) = 0 $=wshShortcut($a_shortcuts[$index],$a_targets[$index]) EndIf next
The reason I brought up the '$' is explained by Gargoyle.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 2220 anonymous users online.
|
|
|