Jump to content

Possibility to let users create own variable through CMD


DavidW
 Share

Recommended Posts

Hello everyone,

I was wondering if there was an command/possibility which let users write their own variable for example in CMD "c:\users\johan\desktop\test.exe [54]".

So that the number that the user typed will be later used in a calculation function.

 

Thanks

Edited by DavidW
Link to comment
Share on other sites

6 minutes ago, RTFC said:

Please read the section "command line parameters" here

Yes i have already done that, But it not answering my question (I could be wrong have low experience in autoIT)

EG; Is there an possibility that i change the "1" in this line to an variable, and whenever the user( who runs the script with a random number) the variable changes to the number given, so the variable is later used for another function

If  $Cmdline[0]>= 1 Then

 

Edited by DavidW
Link to comment
Share on other sites

Sorry, little difficult to understand what you are asking...but if I understand what you are looking to accomplish, yes.

Consider the following:

From the help file link for running scripts
myProg.exe param1 "This is a string parameter" 99

$CmdLine[0] ; This contains 3 parameters.
$CmdLine[1] ; This contains param1.
$CmdLine[2] ; This contains This is a string parameter.
$CmdLine[3] ; This contains 99.

Notice that each parameter has it's own index in the array.  

$CmdLine[0] >= 1 is a good first check as it means at least one parameter was provided.  It will be up to you to verify the parameter(s) values.  You could then switch the condition of the parameter you need.  following the execution example above, let's switch the 3rd parameter and check for 99.

If $CmdLine[0] >= 1 Then
    Switch $CmdLine[3]
        Case 99
            ;Do Stuff
            
    EndSelect
EndIf

 

fuzzier, or clearer?
 

Edited by spudw2k
Link to comment
Share on other sites

Thanks for the explanation, it makes a little bit more sense to me now

But a question, what if i do the following script ?

If $cmdline[0] = 1 Then
    msgbox (0, "", "cool")
if $cmdline[0] = 2 Then
    msgbox(0, "", "niet col")

EndIf
EndIf

and i run the following parameter MyProg.exe [2], why is  the msgbox of $cmdline[0] = 1 appearing?

Edited by DavidW
Link to comment
Share on other sites

What i want to acomplish is the example in JavaScript. So that a user inputs a number in CMD like "Myprog.exe" [number] and then for example the script will run console.log (" with the number from the parameter")

var Number = prompt("Insert number between 0 and 100");

 if (0<Number<50){
       console.log("Low value");
}else if(50<=Number<100){
       console.log("High value");
}else{
       console.log("not a valid number");
}

Do i have to create a loop that checks every number till for example 999? till its reachs the number given in the parameters? or is there an easier way?

Edited by DavidW
Link to comment
Share on other sites

You can for example use the Switch-Case construct to check fixed value ranges:

$number=12
Switch $number
    Case 1 to 50
        msgbox(0,"number = " & $number,"Low number")
    Case 51 to 100
        msgbox(0,"number = " & $number,"High number")
    case Else
        msgbox(0,"number = " & $number,"Invalid number")
EndSwitch

This also supports a sequence of specific values e.g.: Case  1, 3, 5 (do odd number action>, Case 2,4,6 <do even number action>, etc, as well as individual cases, e.g., Case 1 <do value=1 action>, Case 25 <do value=25 action>, etc.

Link to comment
Share on other sites

RTFC thanks for the reponse!,  a couple of questions, already appreciate the help you've done

How do i eventually connect the variable $number to an cmdline parameter? that if the user runs cmd ("myprog.exe /14") the variable $number will be changed to 14

Another question if i may ask. What if want to add another parameter and i want to be able to run the parameters through cmdline so like (Myprog.exe /par 1 (number switch), /par 2 (option to choose if its minus/ equaled by/ plus etc)

Can i just create another switch ? if not could you give me a point in the right direction so i can try to figure it out?

 

 

Edited by DavidW
Link to comment
Share on other sites

spudw2k already pointed you in the right direction. The following crude example checks first if at least one commandline parameter is parsed; if so, it is interpreted as a number that is checked further. It then checks if a 2nd parameter is parsed, and if so, displays it.

if $cmdline[0]>0 Then
    $number=$cmdline[1]
    Switch $number
        Case 1 to 50
            msgbox(0,"number = " & $number,"Low number")
        Case 51 to 100
            msgbox(0,"number = " & $number,"High number")
        case Else
            msgbox(0,"number = " & $number,"Invalid number")
    EndSwitch
EndIf

if $cmdline[0]>1 Then
    $param2=$cmdline[2]
    msgbox(0,"A 2nd parameter was parsed","The second parameter is" & $param2)
EndIf

Of course, if the order of the parsed parameters can be flexible you'd have to create more sophisticated checking, and maybe cycle through all parameters in a For-Next loop to identifiy which is which. I'll leave you to figure that one out for yourself (but search $cmdline or $cmdlineRaw on the forums to find plenty of examples).;)

Edited by RTFC
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...