Jump to content

Pipecmd


dabus
 Share

Recommended Posts

I just like to get output from some cui-commands, so I wrote this little udf (Command.au3).

#include-once

;===============================================================================
;~ command-functions
;===============================================================================

;===============================================================================
; Function Name:  _PipeCMD
;~  0.01    - pipe to $var
;~  0.02    - added non-console-stuff
;~  0.03    - show command
;~  0.05    - write to outputfile
;===============================================================================

Func _PipeCMD ( $a, ByRef $b , $c=0, $d=0, $e=0 ); $a=cmd, $b=$var or Filename, $c=WinMode, $d=show $e=Write to file
    Local $file, $log, $output
    If $c=0 Then
        If $d=0 Then
            $log=Run ( @ComSpec & ' /c ' & $a , '', @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD )
        Else
            $log=Run ( @ComSpec & ' /c ' & $a , '', @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD )
        EndIf
    Else
        If $d=0 Then
            $log=Run ( $a , '', @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD )
        Else
            $log=Run ( $a , '', @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD )
        EndIf
    EndIf   
    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 $e=0 Then
        $b=$output
    Else
        $file=FileOpen ( $b, 10 )
        FileWrite ( $file, $output)
        FileClose ( $file )
    EndIf   
EndFunc

I wrote some simple (silly? :"> ) UDFs before and just would like to know if I would make the grade or not, since I think this would be useful to others, I'd like to share this one... :)

(I hope it's self-explaining.)

I also got a script to copy files and set tasks on network-computers in a domain. Do you think it should be posted or not (I do not want to help spammers ).

Edited by dabus
Link to comment
Share on other sites

(I hope it's self-explaining.)

Errmmm.... It's not!

Use the "standrad" UDF setup template if you really have no idea on what to document:

;===============================================================================
;
; Description:      :
; Parameter(s):     :
; Requirement:      :
; Return Value(s):  :
; User CallTip:     :
; Author(s):        :
; Note(s):          :
;
;===============================================================================

#)

Link to comment
Share on other sites

Header

;===============================================================================
;
; Description:      : Get output of Commands piped to $var or into file
; Parameter(s):    : $a=cmd to run, $b=$var to pipe the output into or filename to put output to
;~                      $c=run cmd in cmd (if it has cui) or not (if it has a gui) 
;                       $d=show the window of the (cmd-)window $e=write output to file
; Requirement:      : AutoIt3 3.1.1 beta 90
; Return Value(s):  : None so far 
; User CallTip:    : _PipeCMD ( "cmd", ByRef "var or filename" [, "gui=1/cui=0" [, "show", ["write into file"]]] ) Reads the output of commands. (required: #include "<Command.au3>" )
; Author(s):          : dabus
; Note(s):          : 
;
;===============================================================================

Sample Script:

#include <Command.au3>
Dim $Output
; 1. Write Output of cmd into $Output
_PipeCMD ( 'net use', $Output )
ConsoleWrite ( $Output & @CR )

; 2. Write Output of cmd into $Output
_PipeCMD ( 'echo dummy', $Output )
ConsoleWrite ( $Output & @CR )

If StringInStr ( $Output, 'dummy') Then; 3. Check for result
; Write Output of cmd into File specified in $Output
    $Output=@TempDir&"\Test.txt"
    _PipeCMD ( 'echo dummy', $Output, 0, 0, 1)
; Open the result
    Run ( 'notepad ' & $Output )
EndIf

It's just useful if you want to use some cui-stuff and you like to check for certain stuff befor you go on. (Like you see in example 2 & 3).

ToDo: Return-Codes :)

Edited by dabus
Link to comment
Share on other sites

Is there a reason that I'm not recognizing for putting a CR in the output on every read loop? I don't know what the typical child EXEs are that you're reading from, but there's no assurance that the child's output writes will end on an end-of-line, so you could have final output tha

t

loo

ks like th

is. :)

Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

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