Jump to content

Command Line Parameters


Recommended Posts

i am writing a script that uses the Run command to execute a EXE file that reads a config file. the name of the config file will change from time to time. therefore in my code i want to replace the name of the file with a variable. i also want to use a command line parameter to pass the name of the current named config file.

i have spent a number of hours trying to figure out how to do that but have come up w/Bubkis.

can someone provide a code example to accomplish this?

here is what i have so far -

so, if my run command looks like this:

Run ($var&"iclient.exe -config " &$var2&"\Policy.xml")

i am thinking that it should look something like this:

$CmdLine[1] is param 1

Run ($var&"iclient.exe -config " &$var2&"\$CmdLine[1]")

i am thinking the command line from DOS or start/Run would look like this:

AutoIT.exe param1=Policy.xml

am i close?

please advise...

Link to comment
Share on other sites

did you read the helpfile. its pretty clear to me.

Command Line Parameters

The special array $CmdLine is initialized with the command line parameters passed in to your AutoIt script.  Note the scriptname is not classed as a parameter; get this information with @ScriptName instead.  A parameter that contains spaces must be surrounded by "double quotes".  Compiled scripts accept command line parameters in the same way.

$CmdLine[0]is number of parameters

$CmdLine[1] is param 1 (after the script name)

$CmdLine[2] is param 2 etc

...

$CmdLine[$CmdLine[0]] is one way to get the last parameter...

So if your script is run like this:

    AutoIt3.exe myscript.au3 param1 "this is another param"

$CmdLine[0] equals... 2

$CmdLine[1] equals... param1

$CmdLine[2] equals... this is another param

@ScriptName equals... myscript.au3

In addition to $CmdLine there is a variable called $CmdLineRaw that contains the entire command line unsplit, so for the above example:

$CmdLineRaw equals... myscript.au3 param1 "this is another param"

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Link to comment
Share on other sites

did you read the helpfile. its pretty clear to me.

<{POST_SNAPBACK}>

yes i did read it and forgive me for not fully understanding the documentation.

can you tell from the code i provided in my request for help that i did not fully understand how to properly accomplish the use of command line parameters?

if you have no assistance to provide then please ignore my request.

Edited by grasshopper
Link to comment
Share on other sites

could you tell me more about your script. (or post the entire thing)

like what is $var and $var2 and iclient.exe

is your script gonna be an .exe if its done

how does ur script get data

how does iclient get data (-config C:/file.xml ?)

etc.

Edited by w0uter

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Link to comment
Share on other sites

could you tell me more about your script. (or post the entire thing)

like what is $var and $var2 and iclient.exe

    the variables can be anything - they are not important to my quest.

is your script gonna be an .exe if its done

    yes

how does ur script get data

    i am hoping to pass data from the command line

how does iclient get data (-config C:/file.xml ?)

    iclient reads the contents of file.xml

    i don't need to write code for that to occur

<{POST_SNAPBACK}>

i was unable to figure this out from the documentation.

so, i am looking for help on how to pass information from the command line into the script and place it in a variable located in the script.

Link to comment
Share on other sites

If $CmdLine[0] > 0 Then

$conffile = $CmdLine[1]

Else

$conffile = ("Prompt", "Enter Configuration File", " M")

EndIf

What needs to be done is to check to see if a parameter was sent, if it has been, set the value of the first parameter to $conffile, else you will be prompted for it.

Make sure that if there are any breaks (spaces) in the file name, you will need to put it in "quotes", else it will be taken as multiple parameters.

I had a script somewhere, I'll look it up though if you need any more help.

Writing AutoIt scripts since

_DateAdd("d", -2, _NowCalcDate())
Link to comment
Share on other sites

If $CmdLine[0] > 0 Then

$conffile = $CmdLine[1]

Else

$conffile = ("Prompt", "Enter Configuration File", " M")

EndIf

What needs to be done is to check to see if a parameter was sent, if it has been, set the value of the first parameter to $conffile, else you will be prompted for it.

Make sure that if there are any breaks (spaces) in the file name, you will need to put it in "quotes", else it will be taken as multiple parameters.

I had a script somewhere, I'll look it up though if you need any more help.

<{POST_SNAPBACK}>

MSLx Fanboy,

i put together a few lines of code to simply pass information from the command line back into the script and then poping up a message box as a test to show that it actually worked. i am listing it below in hopes that you or someone can point out where my syntax errors are occurring.

THE CODE:

Dim $CmdLine[1]

MsgBox(4096, "Command Line Parameter", $CmdLine[1])

THE COMMAND LINE:

C:\Temp\cmdline_params.exe param1 bubkes

Link to comment
Share on other sites

Do not Dim the variable, it is automatically created. Also, just for future reference, when 'Dim'ing array, you need to add '1' to each dimension else it will be too small...

If you remove the 'Dim $CmdLine[1]' line, it should work. You can also do:

For $x = 0 to $CmdLine[0]
 MsgBox(0, "Command Line Parameters", $CmdLine[$x])
Next

That should make a msgbox for each parameter. If you enter

cmdline_params.exe param1 bubkes "this is a parameter" bob

then

$CmdLine[0] = 4

$CmdLine[1] = "param1"

$CmdLine[2] = "bubkes"

$CmdLine[3] = "this is a parameter"

$CmdLine[4] = "bob"

Hope that helps

Writing AutoIt scripts since

_DateAdd("d", -2, _NowCalcDate())
Link to comment
Share on other sites

more advanced.

; CmdLine.exe -a -config "C:\Example\Ex.au3"

;defaults
dim $Config = 'C:\config.cfg'
dim $a = 0

;loop
For $i = 1 to $CmdLine[0]
;if -config is found then get the next value
    If $CmdLine[$i] = '-config' And $i <> $CmdLine[0] Then $Config = $CmdLine[$i+1]
;if -a is found it sets $a to 1
    If $CmdLine[$i] = '-a' Then $a = 1
Next

;msgboxes
dim $msgbox_string = '-config ' & $Config
If $a = 1 Then $msgbox_string &= @CRLF & '-a'
MsgBox(0, '', $msgbox_string)

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...