Page 1 of 1 1
Topic Options
#199647 - 2010-08-24 07:10 PM Supplying arguments to script
Pax Offline
Getting the hang of it

Registered: 2006-12-01
Posts: 51
I am looking at using some existing UDFs which supply arguments, but I'd like to allow users to supply them.

Is there a way to do this as I cannot find anything when doing a basic search in here, nor in the kix scripting guide I have to hand.

Ideally I will be making the script an executable eventually so any method should work with supplying the arguments to the executable as well.

The idea was to use Glenn's copyfiles2 UDF in the script and give this to the service desk so they can push files out to several locations on the network if needed.

It is something which I cannot find it and yet it seems so basic (maybe that is why I cannot find it).

Would a bettwe way be for the script to prompt the user to enter the parameters as part of the script and it can then assign them?

Pax

Top
#199648 - 2010-08-24 07:45 PM Re: Supplying arguments to script [Re: Pax]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
There are at least two ways to pass arguments to Kix scripts..

The first is to actually define the args as Variables on the command line
 Code:
Kix32.exe myscript.kix $ARG1="something" $ARG2="else"
This is easy, but the downside is that the variables you define on the command line are GLOBAL in scope. It's also a bit cumbersome unless you wrap it in a batch file.

The next way is to use the GetCommandLine() function. With this, you can run something like
 Code:
kix32.exe myscript.kix --s:sourcepath --d:destpath
You need to extract this data and put it into your own vars (or use the array). Here's an example adapted from a current project:
 Code:
; Get the source & dest args specified on the command-line

; get the command line as an array - elements 0 & 1 are the kix.exe and the script.kix items
$aCmdLine = GetCommandLine(1)

; scan the array looking for the source arg
$ = AScan($aCmdLine, '--s:', , , 1)		; get SOURC arg
If $ > 1					; 
  $Source = SubStr($aCmdLine[$], 5)
Else						; arg is required - die if not provided
  'ERROR: Source was not specified!' @CRLF @CRLF
  'Usage: myscript.kix --S:Source_path --D:destination_path' @CRLF
  Quit 1
EndIf

; scan the array looking for the destination arg
$ = AScan($aCmdLine, '--d:', , , 1)		; get destination arg
If $ > 1					; 
  $Dest = SubStr($aCmdLine[$], 5)
Else						; arg is required - die if not provided
  'ERROR: Destination was not specified!' @CRLF @CRLF
  'Usage: myscript.kix --S:Source_path --D:destination_path' @CRLF
  Quit 1
EndIf
Using the command line args can be a tad tricky, especially since the built-in kix32.exe parameters can get in the way. Be sure to use "--" as your argument delimiters.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#199650 - 2010-08-25 04:06 PM Re: Supplying arguments to script [Re: Glenn Barnas]
Pax Offline
Getting the hang of it

Registered: 2006-12-01
Posts: 51
Hey Glenn,

Thanks for that. If I am going to convert this to an executable would it still see the elements 0 and 1 as being kix32 and the script file even though it is an executable being run, or should I change the scanning number when I finish writing and debugging to accomodate the new order?

edit: I cannot find the code for the function GetCommandLine() anywhere.

double edit: I see how it is scanning for source and destination so I see I don't need to change anything when making it an executable.

Pax


Edited by Pax (2010-08-25 04:31 PM)

Top
#199651 - 2010-08-25 04:32 PM Re: Supplying arguments to script [Re: Pax]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
GetCommandLine() was introduced in version 4.52 of KiXtart, you may need to upgrade.
Top
#199652 - 2010-08-25 04:43 PM Re: Supplying arguments to script [Re: Richard H.]
Pax Offline
Getting the hang of it

Registered: 2006-12-01
Posts: 51
 Originally Posted By: Richard H.
GetCommandLine() was introduced in version 4.52 of KiXtart, you may need to upgrade.


Ooo yes. I am using 4.51.

Easy done. Thanks for letting me know.

Pax

Top
#199653 - 2010-08-25 06:28 PM Re: Supplying arguments to script [Re: Glenn Barnas]
Pax Offline
Getting the hang of it

Registered: 2006-12-01
Posts: 51
Hi Glenn,

That seems to work fine when I use the source as a directory, but not as a specific file. Is this how the script was intended?

Secondly, when I supply the command as copyfile2($source,$dest,0,1) is that the right format for not creating root directory, but turning on recursion in which case it will copy the file into all the directories within destination and not the destination, as using this seems to copy to the $dest location only? I tried this using copyfiles and copyfile2 functionality.

Lastly, if I want the source to be several directories deep before the files are located (i.e. c:\temp\start\of\directory\files.doc) and I make the source --s:c:\temp then it will copy the directories to $dest\start\of\directory\files.doc and recursively $dest\directory1\start\...\files.doc, $dest\directory2\start\...\files.doc, etc??

Thanks again for all the help,

Pax

Top
#199655 - 2010-08-25 09:02 PM Re: Supplying arguments to script [Re: Pax]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
I think you answered it yourself, but to clarify - the comment is just a reminder that with a command line of
 Code:
kix32 myscript.kix --a --b --c
the array elements will contain:
0 - kix32.exe
1 - myscript.kix
2 - --a
3 - --b
4 - --c

The first two elements are not the actual arguments, but initial elements of the entire command line.

Using AScan does indeed eliminate any issues of where things are in the array.

GetCommandLine is built in as of 4.52.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#199656 - 2010-08-25 09:22 PM Re: Supplying arguments to script [Re: Pax]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Here's some insight:
 Code:
1. CopyFiles2('file', 'dfolder')
will copy the file to a target directory
 Code:
2. CopyFiles2('sfolder', 'dfolder')
will copy the contents of the source folder and all subfolders to the target directory
 Code:
3. CopyFiles2('sfolder', 'dfolder',0,1)
will copy the contents of the source folder but NONE OF THE SUBFOLDERS to the target directory.
 Code:
4, CopyFiles2('sfolder', 'dfolder\sub\sub',1)
will create the destination folder and then copy all of the files and subfolders from the source folder to the target directory.

Imagine that the source folder is C:\temp\a\b\c, and the destination folder is D:\temp. If you use method 2, all of the files and folders in the "c" subfolder will be placed into D:\Temp. If you specify D:\Temp\a\b\c in form 2 AND the subfolders don't exist, the files will be placed directly in D:\Temp, which is why it's a good idea to create the target folder (method 4).

Hope that helps you sort things out. Don't forget to include the DirList UDF in your script, too.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#199661 - 2010-08-26 02:59 PM Re: Supplying arguments to script [Re: Glenn Barnas]
Pax Offline
Getting the hang of it

Registered: 2006-12-01
Posts: 51
Hi Glenn,

I've verified 1,2 and 3 in your list without a problem. Maybe I am looking at this the wrong way, or want this to do something you have not programmed it to, but I want to copy a source directory, or source files into the subdirectories of the destination root. What I mean by that is the source file c:\temp\test.txt copied to y:\ as destination will copy to y:\sub1, y:\sub2 ... y:\sub99.

I am trying to use this to push out files/directories to "team" network locations.

Am I expecting your code to do something you had not intended it to do, or do I need to use it, but have it in the recursive loop which goes through the array of subdirectories to copy the file.

I'm pretty sure I can do the array bit, but just wondering what I should be looking at as far as where your code fits into my intentions.

Pax

Top
#199665 - 2010-08-26 03:28 PM Re: Supplying arguments to script [Re: Pax]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
If I understand you correctly, you want to "distribute" a file or folder to multiple locations, right? Onc source file/folder, multiple destinations. If that's the case, this isn't the tool to use unless you wrap code around it -
 Code:
$aDests = '\path1\', '\path2\'... ; create array of destination paths
For Each $Dest in $aDests
  CopyFiles2($Source, $Dest, 1)
Next
I have other tools that might be better to use. I'm heading to the airport now, but will see if there's anything in my arsenal that's better for you and follow up when I get back this weekend.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#199667 - 2010-08-26 05:21 PM Re: Supplying arguments to script [Re: Glenn Barnas]
Pax Offline
Getting the hang of it

Registered: 2006-12-01
Posts: 51
 Originally Posted By: Glenn Barnas
I have other tools that might be better to use. I'm heading to the airport now, but will see if there's anything in my arsenal that's better for you and follow up when I get back this weekend.

Glenn


Many thanks. We have a public holiday here this weekend, but I'll check back tuesday morning.

Thanks for spending your time on this, and have a good trip.

Pax

Top
#199824 - 2010-09-13 09:56 AM Re: Supplying arguments to script [Re: Glenn Barnas]
Pax Offline
Getting the hang of it

Registered: 2006-12-01
Posts: 51
 Originally Posted By: Glenn Barnas
]I have other tools that might be better to use. I'm heading to the airport now, but will see if there's anything in my arsenal that's better for you and follow up when I get back this weekend.

Glenn


Hi Glenn,

I was wondering whether you got a chance to have a look into your "arsenal"?

Pax

Top
#199825 - 2010-09-13 01:31 PM Re: Supplying arguments to script [Re: Pax]
Pax Offline
Getting the hang of it

Registered: 2006-12-01
Posts: 51
This is what I have done to get this to scan a directory and to copy source data (using Glenn's copyfiles UDF). Yes there is some blatant homage to his work.

I've got it so that once it is all right I uncomment references to copyfiles2 and comment references to copyfiles (as the end user's won't know what to do with the output).

The error I get is

 Code:
ERROR : Error in expression: this type of array not supported in expressions.!
Script: C:\Stuff\CopyCentral\copycentral.kix
Line  : 57

Which is the line "For each", but this code is used directly in the "dirplus" examples. I am using the latest version of kix32 in this.
 Code:
break on
call @ScriptDir +"\dirlist.udf"
call @ScriptDir +"\dirplus.udf"
;call @ScriptDir +"\copyfiles2.udf"
call @ScriptDir +"\msg.udf"
call @ScriptDir +"\copyfiles.udf"

; Get the source & dest args specified on the command-line

; get the command line as an array - elements 0 & 1 are the kix.exe and the script.kix items
$aCmdLine = GetCommandLine(1)

; scan the array looking for the source arg
$ = AScan($aCmdLine, '--s:', , , 1)		; get SOURCE arg
If $ > 1					; 
  $Source = SubStr($aCmdLine[$], 5)
Else						; arg is required - die if not provided
  'ERROR: Source was not specified!' @CRLF @CRLF
  'Usage: myscript.kix --s:Source_path --d:destination_path' @CRLF
  Quit 1
EndIf

; scan the array looking for the destination arg
$ = AScan($aCmdLine, '--d:', , , 1)		; get destination arg
If $ > 1					; 
  $Dest = SubStr($aCmdLine[$], 5)
Else						; arg is required - die if not provided
  'ERROR: Destination was not specified!' @CRLF @CRLF
  'Usage: myscript.kix --s:Source_path --d:destination_path' @CRLF
  Quit 1
EndIf

$destlist = Dirplus($dest,"/ad")

? "Destination list is " + $destlist
For each $pcc in $destlist
	? "PCC is " + $pcc
	? "Destination Directory is " + $Dest + "\" + $pcc
	;$copycentral = CopyFiles2($Source,$Dest + "\" + $pcc)
	$copycentral = CopyFiles($Source,$Dest + "\" + $pcc,0,1)
Next

exit


Pax

Top
#199826 - 2010-09-13 02:16 PM Re: Supplying arguments to script [Re: Pax]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
The DirPlus and DirList UDFs return arrays - you can't put "$DestList" in a print statement without referencing an element of the array. None of your requirements prior required directory enumeration (since the CopyFiles handles that), wo why add them now?

I'm confused by the logic - copy operation is FROM source TO dest, so why run a DIR of the DEST??

Still looking at the code, but a better explaination of what you want to do might help. No code, just the requirements.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#199827 - 2010-09-13 02:58 PM Re: Supplying arguments to script [Re: Pax]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Here's code that I have working:
 Code:
;; KixGenerated: 2010/09/13 08:05:58
break on
;call @ScriptDir +"\dirlist.udf"
;call @ScriptDir +"\copyfiles2.udf"
;call @ScriptDir +"\msg.udf"
;call @ScriptDir +"\copyfiles.udf"

$Recurse = 1			; recursion is ON by default

; Get the source & dest args specified on the command-line

; get the command line as an array - elements 0 & 1 are the kix.exe and the script.kix items
$aCmdLine = GetCommandLine(1)

; scan the array looking for the source arg
$ = AScan($aCmdLine, '--s:', , , 1)		; get SOURCE arg
If $ > 1					; 
  $Source = SubStr($aCmdLine[$], 5)
Else						; arg is required - die if not provided
  'ERROR: Source was not specified!' @CRLF @CRLF
  'Usage: myscript.kix --s:Source_path --d:destination_path' @CRLF
  Quit 1
EndIf

; scan the array looking for the destination arg
$ = AScan($aCmdLine, '--d:', , , 1)		; get destination arg
If $ > 1					; 
  $Dest = SubStr($aCmdLine[$], 5)
Else						; arg is required - die if not provided
  'ERROR: Destination was not specified!' @CRLF @CRLF
  'Usage: myscript.kix --s:Source_path --d:destination_path' @CRLF
  Quit 1
EndIf

; scan the array looking for the recursion flag
$ = AScan($aCmdLine, '--r', , , 1)		; get recursion flag
If $ > 1					; 
  $Recurse = 0
EndIf

If (GetFileAttr($Source) & 16) = 0		; force recursion OFF for single-file copy
  $Recurse = 0
EndIf


; debug messages
' Source: ' $Source ?
'   Dest: ' $Dest ?
'Recurse: ' $Recurse ?

CopyFiles($Source, $Dest, 1, $Recurse)

Quit 0						; must use QUIT with GetCommandLine!!

Running
CF.KIX --S:C:\PROJECT\KIXLIB\DIRPLUS.UDF --D:C:\TEMP\KIXLIB
copies the single file from the source to the destination.
Running
CF.KIX --S:C:\PROJECT\KIXLIB\ --D:C:\TEMP\KIXLIB --R
Copies the contents of KixLib, but not the two subfolders.
Running
CF.KIX --S:C:\PROJECT\KIXLIB\ --D:C:\TEMP\KIXLIB
copies all of the files, including the two subfolders DOCS and OLD.

You'll have to re-enable the CALL statements, as I ran this through KGen to assemble the UDFs and run a Sanity check. Declaring your vars to limit scope would be a good idea, too, especially when using UDFs.

If you want to push this to multiple computers, you could wrap the logic around this to replace C: with '\\' + $TargetComp + '\C$', enumerating a list of target computer names from a file.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#200019 - 2010-09-22 05:27 PM Re: Supplying arguments to script [Re: Glenn Barnas]
Pax Offline
Getting the hang of it

Registered: 2006-12-01
Posts: 51
Hey Glenn,

I've just returned from leave, but as for requirements. The script should copy files and/or directories from the source point to a non-static list of directories on a destination.

The destination in this case is a root share with the list of directories which will be the final destination for the files i.e. c:\temp\test.doc should go into \\fs01\share\dir1\test.doc, \\fs01\share\blah2\test.doc, etc for all directories within \\fs01\share\. Alternately c:\temp\roll\out\test.txt goes to \\fs01\share\dir1\roll\out\test.txt, \\fs01\share\blah2\roll\out\test.txt, etc.

Does that make sense?

Geoff

Top
#200128 - 2010-10-01 03:30 PM Re: Supplying arguments to script [Re: Pax]
Pax Offline
Getting the hang of it

Registered: 2006-12-01
Posts: 51
I seem to have this worked out now using a 'While' 'Loop', so my code looks like

 Code:
;
break on
call @ScriptDir +"\dirlist.udf"
call @ScriptDir +"\dirplus.udf"
;call @ScriptDir +"\copyfiles2.udf"
call @ScriptDir +"\msg.udf"
call @ScriptDir +"\copyfiles.udf"

; Get the source & dest args specified on the command-line

; get the command line as an array - elements 0 & 1 are the kix.exe and the script.kix items
$aCmdLine = GetCommandLine(1)

; scan the array looking for the source arg
$ = AScan($aCmdLine, '--s:', , , 1)        ; get SOURCE arg
If $ > 1                    ; 
  $Source = SubStr($aCmdLine[$], 5)
Else                        ; arg is required - die if not provided
  'ERROR: Source was not specified!' @CRLF @CRLF
  'Usage: myscript.kix --s:Source_path --d:destination_path' @CRLF
  Quit 1
EndIf

; scan the array looking for the destination arg
$ = AScan($aCmdLine, '--d:', , , 1)        ; get destination arg
If $ > 1                    ; 
  $Dest = SubStr($aCmdLine[$], 5)
Else                        ; arg is required - die if not provided
  'ERROR: Destination was not specified!' @CRLF @CRLF
  'Usage: myscript.kix --s:Source_path --d:destination_path' @CRLF
  Quit 1
EndIf

$destdirs = Dir($dest + "\*")
? "Destination list is " + $destdirs

While $destdirs <> "" and @Error = 0
    ? "The next directory is " + $destdirs
    ;$copycentral = CopyFiles2($Source,$Dest + "\" + $destdirs)
    $copycentral = CopyFiles($Source,$Dest + "\" + $destdirs,0,1)

    $destdirs = Dir ()
Loop
exit


Kudos to Glenn for his assistance. I think that this probably is not the most efficient script, as it tries to treat the files in $destdirs as directories and I'll probably fiddle with it hopefully getting it working with DIRLIST or DIRPLUS functions, but I'm happy as it is for now.

Pax

Top
#200134 - 2010-10-01 08:56 PM Re: Supplying arguments to script [Re: Pax]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
 Originally Posted By: Pax
...it tries to treat the files in $destdirs as directories
A look at my DIR() Primer FAQ would show how to differentiate files from folders using (GetFileAttr($basedir+"\"+$name) & 16).

http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=81929#Post81929

That said, DIR() is old school and the UDFs are the way to go.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#200137 - 2010-10-02 05:33 AM Re: Supplying arguments to script [Re: Les]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Just some feedback - DirList uses "old school" dir commands compared to DirPlus, which uses FSO. That said, when I need to identify lots of files (thousands) I shell to DOS and use the DIR command!

I had a project where I needed to determine if a file had changed within a folder structure that contained hundreds of folders and thousands of files. DIR was the fastest, followed closely by DirList. DirPlus was the slowest - but gave me fast access to many of the file's attributes. Just something to consider when planning your update.

As I recall, the folder contained about 6000 files in about 400 folders. DirPlus took 2+ minutes to scan, DirList about 40 seconds, and DIR about 30.

When using DirList, the last char of any folder is a "\", which makes it easy to detect without any additional calls.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#200152 - 2010-10-04 05:32 PM Re: Supplying arguments to script [Re: Glenn Barnas]
Pax Offline
Getting the hang of it

Registered: 2006-12-01
Posts: 51
Interesting to know about the Dirlist UDF, but Dirlist doesn't seem to put the directory elements into an array that while/loop works with, and neither does Dirplus, yet DIR (built-in and not DOS shelled command) does just fine.

You get the error
 Code:
ERROR : Error in expression: this type of array not supported in expressions.!
when using
 Code:
$destdirs = DirPlus($dest,"/ad")
, unless I need to change my while/loop options.

I'm also looking at using a excel file using readexcel2 to read in a match up list of directories and different brands so I can copy to selective destinations.

I think I need to look at displaying a list of unique values for the brand column though for the input validation. I'll create a new thread for that though.

Pax


Edited by Pax (2010-10-04 06:07 PM)

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 677 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.075 seconds in which 0.024 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