Jump to content

See Process after RunWait(@COMSPEC)


Recommended Posts

I am running commands in dos and I would want to see if the command was successfully executed after running it.

Say I use:

RunWait(@COMSPEC " /c " & "cd aaa\bbb\ccc" & "ddd.exe -remove")

The window closes immediatly after running. How do I keep the dos window open and check the line saying "ddd.exe was removed successfully"

EDIT: BTW can som1 explain those crazy ass quotes used in these kinds of commands? Im getting a huge headache what with 8 single quotes here and 10 double quotes there...

Edited by probedrone
Link to comment
Share on other sites

wats /k and wats /c?

If you open command prompt window and type cmd.exe /? it will show you all the switches that you can use.

The right command will be:

RunWait(@COMSPEC & " /k cd aaa\bbb\ccc\ddd.exe -remove")

@COMSPEC = cmd.exe

/c = Carries out the command specified by string and then terminates

/k = Carries out the command specified by string but remains

Edit: Typo

Edited by Danny35d
AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

The quote madness goes something like this.

For text, enclose in doubles "This is text", or singles 'This is text'

When using @Comspec, there may arise a time where your path or command has "White Space" in it. Command Prompt needs to be told about that, usually with double quotes. So, something like this would not require quotes...

cd c:\somedirectory\subdirectory

while this would...

cd "c:\some directory\subdirectory"

Now within @Comspec, you have to declare your Command Prompt command in quotes, normally like this

@Comspec & " /c command"

But, suppose your command was dir, and the path you wanted to have a directory listing of was c:\Program Files\application. If you did this

@Comspec & " /c dir c:\program files\application"

it would likely end up looking for c:\program... not your target. So, you can handily use a single quote on the outside of the whole command, and use doubles within it, like this

@Comspec & ' /c dir "c:\Program Files\appliaction"'

All you need to remember is that either the singles or the doubles are nested inside of the other. Depends on your needs as to which you use on the inside/outside. If you are familiar with SQL grammar, normally it likes the doubles on the outside, indicating this is text, and a set of singles on the inside of string values to indicate this is "quoted" text, which is different. Because quoted text is generally capable of having spaces or Alpha Numeric characters, the parser needs to be told implicitly to use the string "as is", and that it is not a number or some other constant data type. Otherwise, the parser will throw an error because it tried to do something with the AlphaNumeric string value that it cannot do. At least that is how I perceive it.

On the other hand, if you encapsulate your text within variables, all you need to do is concatenate them together. Or, as I recently was shown, there are some functions within AutoIt that will do some of the converting for you when using @Comspec, especially when you are dealing with paths that have "white space".

Complex enough?

Sul

EDIT: Don't forget too that when using command prompt, you can pipe out your data. Maybe not on deleting, I can't remember on that one. All you do is add >filename.txt to the end of your command, and it outputs the command to that named file instead of the screen. You can also use >>filename.txt to append instead of overwrite.

Edited by sulfurious
Link to comment
Share on other sites

I think sulfurious is the closest.

but i dont think the following command will work

RunWait(@COMSPEC " /c cd aaa\bbb\ccc\ddd.exe -remove")

because you cannot CD into a file, especially an EXE file.

I think probedrone was trying to run TWO commands in one line, I.E.

cd aaa\bbb\ccc

and

ddd.exe -remove

and was getting an error because his syntax was wrong as well as his method.

i think he can just do this:

runwait (@comspec & ' /k aaa\bbb\ccc\ddd.exe -remove')

Maybe more info is need on his part to help us ascertain his situation.

-Blademonkey

Edited by blademonkey

---"Educate the Mind, Make Savage the Body" -Mao Tse Tung

Link to comment
Share on other sites

RunWait(@COMSPEC " /c " & "cd aaa\bbb\ccc" & "ddd.exe -remove")

You can do that from command prompt by using a double &&, like this.

cd c:\aaa\bbb&&ddd.exe -remove

or you could go like this

c:\aaa\bbb\ccc\ddd.exe -remove

which MAY need to be wrapped like this

'"c:\aaa\bbb\ccc\ddd.exe" -remove'

Either way, /k will open a new command prompt and leave it open. You can see what is going on.

Maybe you just want to call a batch file instead. Or maybe you need to check if something exists.

Hard to say exactly.

Late,

Sul

EDIT: Oops, forgot that the && trick can sometimes be finicky about other parameters available to the cmd environment, concerning exactly what it does with quotes. Try using this

start hh.exe ms-its:ntcmds.chm::/ntcmds.htm
from a run box and look up the CMD command parameters for more info. Edited by sulfurious
Link to comment
Share on other sites

Batch style, : is a Goto clause, like :Section - :: is like REM

Typical usage in batch is like this in some of my code. Archives, deep in the bowels of my archives :whistle:

@echo off
IF NOT EXIST %WINDIR%\SYSTEM32\CONFIG goto notxp

Rem type the name of the test file server in the nest line
:: Another Type of REM here

set fileserver=192.168.1.2

:assimbegin
cls
Echo.
Echo We must assimilate you.
Echo First, we must assimilate your network card.
Echo Then we must assimilate your harddrive.
Echo Resistance is futile!
Echo Procede with assimilation now.
Echo.
Echo.
Echo        Choose
Echo 1. Assimilate network card.
Echo.
Echo 2. Assimilate harddrive.
Echo.
Echo 3. Run from the Borg!
set chooseassim=
set /p chooseassim=Enter your choice now.
if not '%chooseassim%' == '' set chooseassim=%chooseassim:~0,1%
if '%chooseassim%' == '1' goto assim
if '%chooseassim%' == '2' goto hddassimilation
if '%chooseassim%' == '3' goto exitrun
goto assimbegin

:assim
if exist c:\redonic.txt goto alreadyran
if "%1"=="" goto assim2
ping %1 -n 1 | find "TTL" >nul
if not errorlevel 1 goto letsfrag
ping %1 -w 3000 | find "TTL" >nul
if errorlevel 1 goto begin

:assim2
ping fileserver -n 1 | find "TTL" >nul
if not errorlevel 1 goto letsfrag
ping fileserver -w 3000 | find "TTL" >nul
if errorlevel 1 goto begin


:begin
netsh interface ip show config > mylan.txx
for /f "tokens=4-9 delims= " %%i in ('findstr for mylan.txx') do Echo %%i %%j %%k %%l %%m>>Mycard.txx 
for /f "tokens=3-9 delims= " %%i in ('findstr for mylan.txx') do Echo %%i %%j %%k %%l %%m %%n>>Mycards.txx
findstr /n "interface" Mycards.txx>Cardcnt.txx
for /f "tokens=1 delims=:" %%i in ('findstr : Cardcnt.txx') do Set num=%%i
if '%num%' GTR '1' goto multicard
goto parsetext

EDIT: A most excellent site

Edited by sulfurious
Link to comment
Share on other sites

Oh, nearly forgot about this one. Depending on what your initiial command is when running @Comspec, you could use the /k command along with an "if errorlevel 0" command (using &&) to display the results in the command prompt window before it closes.

Consider these:

RunWait(@ComSpec & " /c notepad.exe&&if errorlevel 0 calc.exe")
RunWait(@ComSpec & " /c notepad.exe&&if not errorlevel 0 calc.exe")
RunWait(@ComSpec & " /k notepad.exe&&if errorlevel 0 calc.exe")
RunWait(@ComSpec & " /k notepad.exe&&if errorlevel 0 @echo Finished")
RunWait(@ComSpec & " /k notepad.exe&&if not errorlevel 0 calc.exe")
Link to comment
Share on other sites

Have a look at $STDERR_CHILD/ $STDOUT_CHILD.

Maybe this is what fits your needs.

#include <Constants.au3>
Local $file, $log, $output
$log=Run ( @ComSpec & ' /c ddd.exe -remove', 'aaa\bbb\ccc', @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD )
While 1
    $line = StdoutRead($log)
    If @error = -1 Then ExitLoop
    $output = $output & @CR & $line
Wend
While 1
    $line = StderrRead($log)
    If @error = -1 Then ExitLoop
    $output = $output & @CR & $line
Wend
If StringinStr($output, "ddd.exe was removed successfully") Then
    MsgBox(0, 'DDD', 'True')
Else
    MsgBox(0, 'DDD', 'False')
EndIf
Edited by dabus
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...