Page 1 of 1 1
Topic Options
#167418 - 2006-09-11 10:20 AM Code optimisation
Pieman Offline
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 Administrator Offline
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 Offline
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 Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Code:

$wshshortcut($a_shortcuts[$Index],$a_targets[$index])


Top
#167422 - 2006-09-11 01:57 PM Re: Code optimisation
Pieman Offline
Fresh Scripter

Registered: 2002-08-20
Posts: 29
Christ you guys are quick...!
OK spotted the obvious mistake thanks Witto, but it's still not working the same error is generated for the same line.

Top
#167423 - 2006-09-11 02:21 PM Re: Code optimisation
Björn Offline
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
#167424 - 2006-09-11 02:30 PM Re: Code optimisation
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
What Bjorn means is this

$Nul = wshshortcut...

By using the name $Nul, almost anyone else looking at your script will understand what you are doing with the variable. Unlike the Golfing that goes on around here to make the code as small and lite as possible.

Top
#167425 - 2006-09-11 02:52 PM Re: Code optimisation
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
also, like I gave the code to scan via indexes.
you changed it to for each.
but you can't rerefence array[index] with for each, so please take the code I provided.

Top
#167426 - 2006-09-11 02:56 PM Re: Code optimisation
Björn Offline
Korg Regular
*****

Registered: 2005-12-07
Posts: 953
Loc: Stockholm, Sweden.
Thanks Gargoyle for the clarification.
Could you tell me the reason for the For each and then For To ?

Top
#167427 - 2006-09-11 03:09 PM Re: Code optimisation
Pieman Offline
Fresh Scripter

Registered: 2002-08-20
Posts: 29
Guys thanks very much the code is working as expected, thou I don't understand what your discussing by the $Nul statement post or Jooel's following comment?
Top
#167428 - 2006-09-11 03:38 PM Re: Code optimisation
Björn Offline
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
#167429 - 2006-09-11 03:54 PM Re: Code optimisation
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
yep.
my comment is that you can't make it work with for-each.
you must use for-to

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 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.068 seconds in which 0.036 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