Robdutoit
(Hey THIS is FUN)
2014-08-07 08:38 PM
how to run the same UDF but with different conditions

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

 Code:
filetime ("d:\startup\source\", "D:\startup\destination\", "test.ini", SValue, "Source filetime test Ini file"

UDF function is as follows
 Code:
;================================================================================================
;   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?


Glenn BarnasAdministrator
(KiX Supporter)
2014-08-07 09:16 PM
Re: how to run the same UDF but with different conditions

Why not use an "Action" parameter to the UDF that determines what process to follow?

The point of a UDF is to have one block of code that can be called repeatedly. If you create many UDFs, you might as well just write inline code and dispense with the UDFs entirely!
 Code:
Function Example(source, dest, action)

  ; special actions
  If $Action & 1   ; delete/create dest
    RD /s $Dest
  EndIf
  If $Action & 2   ; special action
    ; some stuff
  EndIf
  If $Action & 4
    ; some other stuff
  EndIf
  If $Action & 8
    ; set special parameters..
  EndIf
  ; now do all the standard stuff...

EndFunction
just to provide some ideas..

With bitwise values like this, one Action value can represent 256 combinations 8 unique settings - a value of 5 would del/create AND do "some other stuff". You can also have the UDF exit without doing anything, so all of the conditional logic is in the UDF and you don't have to write logic to determine if you should call the function.

EG. Call the function with the developed arguments. If the args are not validated (present, in scope, etc), the UDF just exits.

Glenn


Robdutoit
(Hey THIS is FUN)
2014-08-08 12:15 AM
Re: how to run the same UDF but with different conditions

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:

 Code:
;================================================================================================
;   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.


Glenn BarnasAdministrator
(KiX Supporter)
2014-08-08 03:53 AM
Re: how to run the same UDF but with different conditions

Multiple IF may be more complex than CASE, but it allows multiple options to be defined. Your CASE allows ACTION to be a single integer value representing ONE function.

With multiple IF statements comparing the Bit value, ACTION can be a number from 0 to 255 and will allow 8 options to be on/off in up to 256 combinations. Higher values for ACTION = more options and combinations.

Glenn


LonkeroAdministrator
(KiX Master Guru)
2014-08-08 05:19 AM
Re: how to run the same UDF but with different conditions

Eh. For kixtart case works just like if.

Glenn BarnasAdministrator
(KiX Supporter)
2014-08-08 04:09 PM
Re: how to run the same UDF but with different conditions

Wha??

Only one match in a Case statement structure, but with multiple IF statements, none, any, or all can be matched.

Glenn


LonkeroAdministrator
(KiX Master Guru)
2014-08-08 04:44 PM
Re: how to run the same UDF but with different conditions

uhm. I think I get what you mean now. yes, you are correct if-endif construct different.

cases in select-block are basically if-elseif-elseif-elseif-elseif construct. the actual matching is equal to if matching, but the logic flow is different. which you were saying and I had read differently.