Jump to content

The Correct Way To Run Dos Commands


Recommended Posts

if i want to do "ping 10.0.0.1 -t" in cmd mode.

i just use

run("ping 10.0.0.1 -t")

but i have just seen this

To run DOS (console) commands, try RunWait(@ComSpec & " /c " & 'commandName', "", @SW_HIDE)

so i use this

runwait( @comspec & " /c ping 10.0.0.1 -t","",@SW_HIDE)

which is the correct or better way to run Dos commands?

Link to comment
Share on other sites

If it is a internal command you have to use @comspec otherwise you can use Run()

Usually I prefere using always @comspec since it seems more tidy to me. Also it is more bulletproof, some commands seems dislike Run() and other are internal in Win2000 and external in W9x.

Link to comment
Share on other sites

If you use the ComSpec method as you wrote it, it will run in a hidden comand window. However, you will have no way to get the output from the window unless you pipe it to a file (example, run @ComSpec & " /c dir > directory.txt").

However, I think I have a better solution for you. Do you only care about the success or failure of the ping? If so, consider using RunWait for your ping (without the @ComSpec, so just RunWait("ping 10.0.0.1") ,which will return the error code of the process you launched when it finishes. If you store this return to a variable, you can gether the success or failure of the ping.

Using the method you posted at the bottom is pointless because it is going to run a command that will never end (due to the -t switch), it will run in a hidden window, and it will not dump the output to any file. It's just eating CPU cycles and resources on your system. A hidden window is only used when you don't need any output, or you're piping the output to a file to read later.

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

Usually I prefere using always @comspec since it seems more tidy to me. Also it is more bulletproof, some commands seems dislike Run() and other are internal in Win2000 and external in W9x.

<{POST_SNAPBACK}>

But you can't get the return (see note) out of that method. There are times when you don't want to use it. Also, because of the problem random8011 mentioned above, if you want the program's window to be hidden from the start, you need to run the handler app directally, and not through ComSpec. Some things cannot be done by passing it through the command interperter.

It's not a matter of how "neat" it looks, but if you need the error code, and what else you want to do.

Edit (note): By "return" I mean the exit code of the process that was launched; all you will get if you use @ComSpec is the exit code of the dos window, and this can (should???) be different than the exit code of the application that you ran through the command interperter.

Edited by pekster

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

If you use the ComSpec method as you wrote it, it will run in a hidden comand window.  However, you will have no way to get the output from the window unless you pipe it to a file (example, run @ComSpec & " /c dir > directory.txt").

i am so sorry i made a wrong example.

i really want to do like this:

run this Dos command in my scirpt:

regedit -s oracle.reg

first i write this

1.runwait("regedit -s oracle.reg")

i tested it,seems ok.

But in my two scripts,both have 1 .

one script takes effect,another doesn't.

SO i write this:

2.runwait( @comspec & " /c regedit -s oracle.reg","",@SW_HIDE)

2 takes effect in both two scripts.

i don't understand why 1 doesn't take the same effect in my two scripts.

and,i want to know the different between 1 and 2 function?

i am sorry for my poor english...........

Link to comment
Share on other sites

  • 3 months later...

@comspec look in the whole %Path% of Windows

runwait() only in the @workingdir (if not path are present, of course)

Ay least... I think so.

<{POST_SNAPBACK}>

@comspec is a path to cmd.exe

RunWait is a function that once called, autoit script pauses execution until executed program exits.

Run is a function that doesn't pause autoit script execution after it's execution. The script continues execution at the same time as the Runned program Runs.

Link to comment
Share on other sites

  • 5 years later...

@comspec is a path to cmd.exe

RunWait is a function that once called, autoit script pauses execution until executed program exits.

Run is a function that doesn't pause autoit script execution after it's execution. The script continues execution at the same time as the Runned program Runs.

j'ai presque le meme probleme, je veux lancer la commande dos

isql -User -Pass -SSql -i D:\dev\db_history.sql -Dsystem > DBHistory.out

j ai essayé comme ca mais ca marche pas

run (@ComSpec & "/c isql -User -Pass -SSql -i D:\dev\db_history.sql -Dsystem > DBHistory.out")

Link to comment
Share on other sites

@comspec is a path to cmd.exe

RunWait is a function that once called, autoit script pauses execution until executed program exits.

Run is a function that doesn't pause autoit script execution after it's execution. The script continues execution at the same time as the Runned program Runs.

j'ai presque le meme probleme, je veux lancer la commande dos

isql -User -Pass -SSql -i D:\dev\db_history.sql -Dsystem > DBHistory.out

j ai essayé comme ca mais ca marche pas

run (@ComSpec & "/c isql -User -Pass -SSql -i D:\dev\db_history.sql -Dsystem > DBHistory.out")

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