#209378 - 2014-08-07 08:38 PM
how to run the same UDF but with different conditions
|
Robdutoit
Hey THIS is FUN
Registered: 2012-03-27
Posts: 363
Loc: London, England
|
This is probably a very silly question!
I am busy tidying up my kixtart scripts and I can't see how to make a UDF run if it meets a specific condition.
For example my coding to compare file times and copy from source if both files do not have the same time is as follows:
main script
filetime ("d:\startup\source\", "D:\startup\destination\", "test.ini", SValue, "Source filetime test Ini file" UDF function is as follows
;================================================================================================
; This function checks the file time and date and updates files if both files are not the same time
;================================================================================================
Function filetime($Installs, $DestDir, $File, $Value, $ProgorProfile)
$Value = COMPAREFILETIMES ($Installs + $File, $Destdir + $File)
if $Value <> 0
? "Attempting to install $ProgorProfile"
? "Copying Files to Install $ProgorProfile"
copy $Installs + $File $DestDir + $File
Else
? "$ProgorProfile Already installed"
Endif
EndFunction
Now lets say that I have another file in the main script that I want to comparefiletimes, but I want to copy the file if the source file is newer than the destination file and I want to delete the destination subdirectory and files and recreate the subdirectory.
I can easily copy the function and replace if $Value <> 0 with if $Value = 1 or $Value = -3 and add a RD $Destdir /S and MD $DestDir code, but I am trying to get rid of functions that are practically identical apart from one/two lines.
I have the same problem with another function where the function is reading a file and then either installing a printer based on names from that file or editing the registry based on the names or setting the default printer based on the names in the file being read. So I have four or five different functions reading a file and doing something slightly different with the results of the read. I don't think I can create two different functions as the readline part of the function has a loop so I need to stay within the function.
Or would you recommend that I have one line of code in the main script and a dozen different UDF's that are practically identical apart from one or two lines?
|
Top
|
|
|
|
#209384 - 2014-08-08 12:15 AM
Re: how to run the same UDF but with different conditions
[Re: Glenn Barnas]
|
Robdutoit
Hey THIS is FUN
Registered: 2012-03-27
Posts: 363
Loc: London, England
|
Yes Glenn, I agree with you 100%. The point of a UDF is to have one block of code that can be called repeatedly. Quite a few of my UDF's are used and re-used quite a bit.
This is what prompted the question as I saw no point in these particular UDF's where effectively I am rewriting the code over and over again with some minute changes.
I have just realised that your action example is very similar to my case function that I have, so I have used the case function so as to reduce the number of If, else endif statements.
I ended up with this:
;================================================================================================
; This function checks the file time and date and updates files if both files are not the same time
;================================================================================================
Function filesametime($Installs, $DestDir, $File, $Value, $Action)
$Value = COMPAREFILETIMES ($Installs + $File, $Destdir + $File)
SELECT
Case $Action = 1 ; copies file if not same time
if $Value <> 0
? "Running Action one"
copy $Installs $DestDir /s
Else Endif
Case $Action = 2 ; remove subdirectory and create new directory and updates only if source file is newer than dest file
if $Value = 1 or $Value = -3
? "Running Action two"
RD $Destdir /S
MD $DestDir
? "Copying Files to Install $ProgorProfile"
copy $Installs $DestDir /s
else endif
ENDSELECT
EndFunction
This is better as it will cut down on the repetitive coding. The common coding will come in handy with the readline file UDF. I don't know why I didn't think of using a case in the UDF!
Thank you.
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 557 anonymous users online.
|
|
|