Jump to content

Command line executions using AutoIT


Recommended Posts

I want to run few commands that successfully run on the Command Prompt console. However when I try to execute them using AutoIT, it doesn't seem to work.

#RequireAdmin

RunWait(@ComSpec & "SET ABC=C:\Some\Folder","",@SW_MAXIMIZE)
RunWait(@ComSpec & "SET DEF=\\Server\Shared\Path","",@SW_MAXIMIZE)
RunWait(@ComSpec & " /c " & "%DEF%\File.bat","",@SW_MAXIMIZE)
RunWait(@ComSpec & "ping google.com","",@SW_MAXIMIZE)

Also, how do I get the output of last command in a message box?

My overall intent is to simply see the output of the last command. I don't care where rest of it happens. But all these are connected.

 

I've also tried other options like running it without @ComSpec or running it with " /c ".

Edited by pranaynanda
Link to comment
Share on other sites

this should be more clear :

;Auto close
RunWait(@ComSpec & " /c " & "ping google.com","",@SW_MAXIMIZE)

;Need be manually closed
RunWait(@ComSpec & " /k " & "ping google.com","",@SW_MAXIMIZE)

;Return a msgbox with result
#include <Constants.au3>
Global $DOS, $Message
$DOS = Run(@ComSpec & " /k " & "ping google.com","",@SW_MAXIMIZE, $STDERR_CHILD + $STDOUT_CHILD)
ProcessWaitClose($DOS)
$Message = StdoutRead($DOS, True)
MsgBox(0, "Stdout Read:", $Message)

 

 

Link to comment
Share on other sites

OR:

Local $BATFile = "\\Server\Shared\Path\File.bat" ; Set path to var
If FileExists($BATFile) Then RunWait(@ComSpec & " /c " & $BATFile, "", @SW_MAXIMIZE)

Local $iPID = Run(@ComSpec & " /c " & "ping google.com", "", @SW_HIDE, 6)
ProcessWaitClose($iPID) ; Wait until the process has closed using the PID returned by Run.
Local $sOutput = StdoutRead($iPID) ; Read the Stdout stream of the PID returned by Run. This can also be done in a while loop. Look at the example for StderrRead.
MsgBox(0, "Stdout Read:", $sOutput) ;Return a msgbox with result

 

Regards,
 

Link to comment
Share on other sites

@Synapsee that did not work. I understand that It is supposed to but that certainly did not go as expected. It only returns a message box with no text and an Ok button. I think @Trong your code might as well run the similar way. I wanna tell you guys that the output is a slow on the client and instantaneous on the server (I guess that's just latency issues) when the statements are run one after another by manually running them in a command prompt console. And those are successful outputs. I cannot seem to understand how to get them in a message box. I can experiment with a list box too but I'm not sure how that will go. This custom tool is something like 'net user'. It produces two columns. One of the active user, the other of machine or the node.

Link to comment
Share on other sites

@pranaynanda,

for first, you cannot set environment variables across cmd sessions. even if your SET syntax was correct (which it isn't), after your script is done with the first RunWait() to set %ABC%, that cmd session ends, and with it the existence of %ABC%. same goes for %DEF%. so when your script reaches the 3rd RunWait(), actually %ABC and %DEF% do not exist.

if you do things in AutoIt, do it the AutoIt way - define the variables in AutoIt, and use only a single call to RunWait().

also, when running external utilities, you do not need to use @comspec. this modification on the code by @Synapsee works just as well:

#include <Constants.au3>
Global $DOS, $Message
$DOS = Run("ping google.com","",@SW_MAXIMIZE, $STDERR_CHILD + $STDOUT_CHILD)
ProcessWaitClose($DOS)
$Message = StdoutRead($DOS, True)
MsgBox(0, "Stdout Read:", $Message)

 

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

Thank you. I did not know that previously that when the cmd sessions end, along end the existence of the environment variables.

10 minutes ago, orbs said:

if you do things in AutoIt, do it the AutoIt way - define the variables in AutoIt, and use only a single call to RunWait().

Could you please tell me how do I do it then? I have no ideas.

Link to comment
Share on other sites

Hi.

As orbs stated already: Use Autoit Variables in stead of System Variables.

Anyway, if you want to go the complicated detour (ab)using the Windows Environment, then lookup in the helpfile the topic covering EnvUpdate()

And perhaps you might want to post the current state of your code ;-)

 

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

@pranaynanda,

working with your first example, this is how you would define your variables and use them in AutoIt:

$ABC = "C:\Some\Folder"
$DEF = "\\Server\Shared\Path"
RunWait(@ComSpec & " /c " & $DEF & "\File.bat", "", @SW_MAXIMIZE)
RunWait("ping google.com", "", @SW_MAXIMIZE)

now, why won't you go exploring the wonderful help file of AutoIt?

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

Okay Guys, Thanks for all that help. The function seems to be running fine because if I assign the value of RunWait to a variable and get that on the messagebox, the returned value is 1. However, I am unable to print the output that is generated on the CMD console when the tool is run to a message box.

One more thing I would like to point out that the function returns with random four digit numbers if I use Run() instead of RunWait().  Also, the return code is 0 if I do not use @ComSpec.

Edited by pranaynanda
Link to comment
Share on other sites

Another update here, the 3rd and 4th commands are somewhat related. The batch file called in the 3rd command sets the prerequisites for the output of 4th. I think the existence of the variables set in 3rd command is terminated. Also, now I am able to get some output, but the one that is not desired (somewhat an error message).

Edited by pranaynanda
Link to comment
Share on other sites

Okay, if anybody can get me across this, I'll be done with it (Most Likely). I've understood what you all said. I just want to keep the session open until the last command is run. On diagnosis, I found that I wasn't getting the desired output because the work done by the 3rd command was lost after it was executed and wasn't available when the 4th command was run.

Edited by pranaynanda
Link to comment
Share on other sites

@pranaynanda, you are making it far more complicated than it should be.

if you have to operate a DOS session (rather than merely launch an application), then do so in the DOS session.

meaning, put your four commands in a single batch file, then call this batch file via AutoIt, and capture the output as demonstrated in post #2.

EDIT: i will also be mighty disappointed if i found out that all you need from the output is to write it to a file. guess why? :-)

Edited by orbs

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

@orbs I know it would have been much easier then, but I only want a single .exe. I don't want to run a DOS session. It's just that the output comes perfectly on a command prompt session. The thing is working great. It's just that the work done by the third one isn't available for 4th so when 4th is run, it says that no connection to db exists because that's what the 3rd one is supposed to do. I know, had it been just writing to file, it'd have hardly been a concern.

Link to comment
Share on other sites

2 minutes ago, pranaynanda said:

I don't want to run a DOS session

if your application depends on environment variables, then you pretty much have to. besides, why not? you can run it hidden if aesthetics concerns you.

4 minutes ago, pranaynanda said:

I only want a single .exe

no problem, the exe can write the batch file and then run it.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

Could you please be more specific and tell me how? It's like my brain is smoked in this puzzle.

5 minutes ago, orbs said:

if your application depends on environment variables, then you pretty much have to. besides, why not? you can run it hidden if aesthetics concerns you.

This is how my code looks right now

#include <Constants.au3>
Global $DOS, $Message, $sOutput

$ABC = "C:\Some\Path"
$DEF = "\\Server\Shared"
MsgBox(0,"Tes",$ABC & @CRLF & $DEF)

;RunWait(@ComSpec & " " & " /c " & "SET ABC=C:\Some\Path")
;RunWait(@ComSpec & " " & " /c " & "SET DEF=\\Server\Shared")

Local $BATFile = $DEF & "\FileName.bat" ; Set path to var
MsgBox(0," ", $BATFile)
If FileExists($BATFile) Then
    $c=RunWait(@ComSpec & " " & "/c " & $BATFile, "", @SW_MAXIMIZE, $STDERR_CHILD & $STDOUT_CHILD)
    ;ProcessWaitClose($c)
    MsgBox(0,"Test",$c)
EndIf
;RunWait("%DEF%\FileName")
;$iPID=RunWait(@ComSpec & '" /k ' & 'utility -opt=1 -opt-2 -opt=3')

$List= $ABC & "\Folder\utility.exe"
If FileExists($List) Then
    $DOS = Run($List & " utility -opt=1 -opt-2 -opt=3","",@SW_MAXIMIZE, $STDERR_CHILD & $STDOUT_CHILD)
    MsgBox(0,"Test",$DOS)
    $d=ProcessWaitClose($DOS)
    $Message = StdoutRead($DOS, True)
    MsgBox(0, "Stdout Read:", $Message & @CRLF & $d)
EndIf
;#ce

It is a bit messy because I tried to do some extensive R&D. Learning by self is a hard job!

Link to comment
Share on other sites

what is FileName.bat doing?

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

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...