Jump to content

Run() command fails with certain commands


Recommended Posts

I have a function that runs a DOS command and returns the output of the command, and it returns the correct data for some commands, bu tnot for others.  I've narrowed it down to this:
"HELP FIND" returns nothing
"HELP DIR" returns several strings of help info.

    Here's my test code:

#include <Constants.au3>

test()
Func test()
    Local $sSTDOUT, $cmd

    $cmd = "C:\Windows\System32\help.exe find"
    ;$cmd = "C:\Windows\System32\help.exe dir"
    $sSTDOUT = _RunDosCmd($cmd)

    ConsoleWrite("+++: $sSTDOUT ==>" & $sSTDOUT & "<==" & @CRLF)
    MsgBox(0, "INFO", "$sSTDOUT ==>" & $sSTDOUT & "<==")
EndFunc   ;==>test

Func _RunDosCmd($sDosCmd)
    Local $cmd, $iPID, $sSTDOUT, $ar, $err

    $cmd = @ComSpec & ' /C ' & $sDosCmd

    ConsoleWrite("+++: $cmd = " & $cmd & @CRLF)

    ; Run the command, capturing the STDOUT data
    $iPID = Run($cmd, "", @SW_HIDE, $STDERR_MERGED)
 ; <== merge STDOUT and STDERR 
    ; Success: the PID of the process that was launched.
    ; Failure: 0 and sets the @error flag to non-zero.
    $err = @error
    ConsoleWrite("+++: $err  = " & $err & " after call to Run()" & @CRLF)
    ConsoleWrite("+++: $iPID = " & $iPID & " after call to Run()" & @CRLF)
    If ($err) Then
        ConsoleWrite("+++: Error returned by Run()" & @CRLF)
        Return ("")
    EndIf

    ; Wait until the process has closed using the PID returned by Run.
    ProcessWaitClose($iPID)
    ; Success: 1 and sets @extended to the exit code of the process.
    ; Failure: 0 if the wait timed out. On invalid PID the @error flag
    ; is set to non-zero and @extended is set to 0xCCCCCCCC.
    $err = @error
    ConsoleWrite("+++: @error = " & $err & " after call to ProcessWaitClose()" & @CRLF)

    ; Read the merged STDOUT/STDERR stream of the PID returned by Run.
    $sSTDOUT = ""
    While (1)
        $sSTDOUT &= StdoutRead($iPID)
        $err = @error
        If ($err) Then ExitLoop
    WEnd

    Return ($sSTDOUT)
EndFunc   ;==>_RunDosCmd

 

Link to comment
Share on other sites

  • Moderators

Hi, AndyS19. Can you please explain the need to mix languages? Just curious if you're just teaching yourself (which is great, welcome!) or if you have some requirement that you use shell commands.

   If you want to find string in a file or files like help find, there are functions in AutoIt to do that: take a look at the File functions in the help file.

   And you can use _FileListToArray or _FileListToArrayRec as a replacement for help dir.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

$iPID = run('cmd /c echo | help find' , "" , @SW_HIDE , 0x8)

$sOutput = ""

 While 1
        $sOutput &= StdoutRead($iPID)
        If @error Then
            ExitLoop
        EndIf
    WEnd

msgbox(0, '' , $sOutput)

 

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

JLogan3o13:
The reason is speed.  I've written an AutoIT cross reference script.  The script works, but does not take into consideration the #include files.  I'm trying to update the script to do that.

The _FileListToArray or _FileListToArrayRec functions read the Names of a list of files.  Not what I want. I want to fill an array with the lines in the matching files that contain a pattern ("#include").  To use the _fFileList... functions, for each file in the returned array, I would have to read its lines into an array, then use something like _ArrayFindAll() to extract an array of indicies of matching lines, then parse that array to extract each line and add it to another array.

Instead, 1 call to the dos 'FIND "#include" C:\Temp\*.au3' does it in 1 step.

I assume that it's also faster to go the DOS way, unless there's a better way.

Link to comment
Share on other sites

boththose:

Regarding your little script, I tried that and it worked.  However, I've been involved with MSDOS for years and I cannot figure out why "ECHO | some-DOS-command" solves the problem.   ECHO by itself just says whether ECHO is on or off (ECHO is on.).  Do you know the reason it works?

Link to comment
Share on other sites

It doesnt matter what command you use first, echo is just quick everytime.  My guess was that there is something boned with the stream, and that behavior did not exist when piped through a command that opens the stream as expected, but that guess is wrong as evidenced by:

$iPID = run('cmd /c help find | help find' , "" , @SW_HIDE , 0x8)

 

and the fun palndromic

$iPID = run('cmd /c find help|help find' , "" , @SW_HIDE , 0x8)

 

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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