Jump to content

Understanding $CmdLine


Recommended Posts

Hello,

I am trying to understand the $CmdLine array. My goal is to run scriptA.exe have it create a variable $varA and then run scriptB.exe and have scriptB.exe output $varA to a Msgbox. It isn't working though, so I am not getting how this works.

This is what I am trying:

ScriptA -

$varA = 'Test'
Run('d:\(x86)\AutoIt3\AutoIt3.exe "D:\AutoIT Script\CMDLINE\scriptB.au3"')

ScriptB -

If $CmdLine[0] = 0 Then
MsgBox(64, "Result", "No variable was found.")
Else
MsgBox(64, "Variable", $CmdLine[1] & $varA)
EndIf

If someone can help me understand why this isn't working, I'd appreciate it.

Link to comment
Share on other sites

You have to pass $varA to the Run command as parameter.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Run(@Autoitexe & '/AutoIt3ExecuteScript & "D:AutoIT ScriptCMDLINEscriptB.au3 "' & $varA)

Should work. Especially if the first program is already compiled, and if not will look for the autoit3.exe file and run that instead.

EDIT: forgot the code block :)

BTW, don't put variables inside the quotes, otherwise they become strings and not variables.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

A space is necessary between "scriptB.au3" and $varA as BrewManNH showed in his example.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Thanks for your help BrewManNH and Water.

So I compiled both scripts, because running them from Scite still wasn't working. However, even compiled the variable is never passed from scriptA to ScriptB.

Here are my compiled scripts:

ScriptA:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
Global $varA = 'Test'
Run(@Autoitexe & '/AutoIt3ExecuteScript & "D:AutoIT ScriptCMDLINEscriptB.au3 "' & $varA)
Run('"D:AutoIT ScriptCMDLINEscriptB.exe "' & $varA)

ScriptB:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
Global $varA
If $CmdLine[0] = 0 Then
MsgBox(64, "Result", "No variable was found.")
Else
MsgBox(64, "Variable", $CmdLine[1] & @CR & $varA)
EndIf
MsgBox(0,"",$varA)

Maybe if I understood what is happening better I could figure this out.

So I first declare a variable in Script A

I then run ScriptB using Run("ScriptB.exe " & varA) and at the end I place the variable that I want transferred.

Now I'm confused. How does this variable become recognised by ScriptB? What does CmdLine do to store the variable in it's array, or how does it store it in it's array?

Maybe it doesn't and that may explain why this isn't working because I am way off track.

Edited by jazzyjeff
Link to comment
Share on other sites

What does the msgbox in script B show when you run it?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

$varA in ScriptB will always be empty because you never assign a value to the variable.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

The first box I see says "No variable found" which must be because there are values stored in $CmdLine. The 2nd Msgbox is empty because $varA doesn't have a value.

:-) I get what your saying Water and I see why $varA has no value because I don't assign anything to it. What's missing to get $varA from scriptA to scriptB? I've tried assign $cmdline[1] to the value of $varA, but that doesn't work either.

I know I could always do a FileWrite and have the variable sent to a txt file and have the other script read the txt file and then delete it. It just seems like it'd be cooler to just have the variable passed between the scripts though.

Link to comment
Share on other sites

Small problem in the post I made, the spaces/quotes are in the wrong place. I hadn't tested it because I'm at work and in the middle of something, but got a few minutes to tweak it.

Here's the corrected code.

; ScriptA
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
Global $varA = 'Test'
Run(@Autoitexe & ' /AutoIt3ExecuteScript ' & '"scriptB.au3" ' & $varA) ; space needed AFTER the double quote and before the single quote
Run('"D:AutoIT ScriptCMDLINEscriptB.exe" ' & $varA)

; ScriptB
#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
Global $varA
If $CmdLine[0] = 0 Then
     MsgBox(64, "Result", "No variable was found.")
Else
     $varA = $CmdLine[1] ; assign the passed parameter to $varA
     MsgBox(64, "Variable", "$CmdLine[1] = " & $CmdLine[1] & @CR & "$varA = " & $varA)
EndIf
MsgBox(0, "", $varA)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Ah man that was such a basic oversight on my part! I'm sorry you spent time on that, but I totally appreciate it.

It works.

Water, BrewmanNH, thank you both. I feel like I understand how this works now. This will be really cool for developing/improving my other scripts.

Link to comment
Share on other sites

BrewmanNH was too fast. Just wanted to post a similar code.

Glad it works for you now!

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

When dealing with quotes for a command line for Run/ShellExecute etc., I find that the easiest way to tell if the command line is correct is to use a ConsoleWrite on the command's parameters and run it from SciTE, if it looks ok there, then I know I've gotten the quotes/spaces right. Which is how I found out that the space was in the wrong place originally.

This:

@Autoitexe & ' /AutoIt3ExecuteScript ' & '"scriptB.au3" ' & $varA = F:PortableAutoIt3.3.8Appautoit3.exe /AutoIt3ExecuteScript "scriptB.au3" Test

Is what is should look like.

This:

@Autoitexe & '/AutoIt3ExecuteScript & "D:AutoIT ScriptCMDLINEscriptB.au3 "' & $varA = F:PortableAutoIt3.3.8Appautoit3.exe/AutoIt3ExecuteScript & "D:AutoIT ScriptCMDLINEscriptB.au3 "Test

Is what it looked like from what I posted the first time.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Another suggestion:

Write the Return Value plus the content of the macros @error and @extended to the console after each function call.

Global $iResult = Run(@Autoitexe & ' /AutoIt3ExecuteScript ' & '"scriptB.au3" ' & $varA)
ConsoleWrite("Return value: " & $iResult & ", @error: " & @error & ", @extended: " & @extended & @CRLF)

Run gets the PID of the started process. If you get 0 then there was an error. This makes sure you start to search at the right location and get all the information necessary for debugging.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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

×
×
  • Create New...