Jump to content

Autoit CMD


Trong
 Share

Recommended Posts

Examples of CMD in Autoit GUI!

#include <GUIConstants.au3>
#include <Constants.au3>

Global $hGUI = GUICreate("AutoIt CMD", 604, 320, -1, -1)
Global $cCommand = GUICtrlCreateInput("ping google.com", 64, 10, 416, 21)
GUICtrlCreateLabel("CMD: >", 20, 15, 40, 20)
Global $cExecute = GUICtrlCreateButton("Execute Command!", 485, 8, 112, 25)
Global $cOutputBox = GUICtrlCreateEdit("", 10, 47, 578, 260, $ES_READONLY + $ES_MULTILINE + $WS_HSCROLL + $WS_VSCROLL + $ES_AUTOVSCROLL) ;$ES_READONLY=2048, $ES_MULTILINE=4, $WS_HSCROLL=0x00100000, $WS_VSCROLL=0x00200000, $ES_AUTOVSCROLL=64
GUICtrlSetData(-1, "")
GUISetState(@SW_SHOW)

Global $sCommand

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE ; $GUI_EVENT_CLOSE=-3
            Exit
        Case $cExecute
            $sCommand = GUICtrlRead($cCommand)
            If StringStripWS($sCommand, 8) <> "" Then
                GUICtrlSetState($cCommand, $GUI_DISABLE) ;$GUI_DISABLE=128
                GUICtrlSetState($cExecute, $GUI_DISABLE)
                GUICtrlSetData($cExecute, " eXecuting..... ")
                _ExecuteCommandStream($sCommand)
;~              GUICtrlSetData($cOutputBox, _ExecuteCommand($sCommand))
                GUICtrlSetData($cExecute, "Execute Command")
                GUICtrlSetState($cExecute, $GUI_ENABLE) ; $GUI_ENABLE=64
                GUICtrlSetState($cCommand, $GUI_ENABLE)
            EndIf
    EndSwitch
WEnd

Func _ExecuteCommandStream($sCommand)
    Local $cCommandinfo = Run('"' & @ComSpec & '" /c ' & $sCommand, @ScriptDir, @SW_HIDE, $STDERR_MERGED) ;$STDERR_MERGED=8
    Local $sOutput, $sOutputError
    While 1
        GUICtrlSetData($cOutputBox, $sOutput & @CRLF)
        $sOutput &= StdoutRead($cCommandinfo)
        If @error Then ExitLoop
        Sleep(50)
    WEnd
    GUICtrlSetData($cOutputBox, $sOutput & @CRLF)
EndFunc   ;==>_ExecuteCommandStream

Func _ExecuteCommand($sCommand)
    Local $cCommandinfo = Run('"' & @ComSpec & '" /c ' & $sCommand, @ScriptDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD + $STDIN_CHILD) ;$STDERR_CHILD=4, $STDOUT_CHILD=2, $STDIN_CHILD=1
    Local $sOutput, $sOutputError
    While 1
        $sOutput &= StdoutRead($cCommandinfo)
        If @error Then ExitLoop
    WEnd
    While 1
        $sOutputError &= StderrRead($cCommandinfo)
        If @error Then ExitLoop
    WEnd
    If $sOutput <> '' Then
        Return $sOutput
    ElseIf $sOutputError <> '' Then
        Return SetError(0, 1, $sOutputError)
    Else
        Return SetError(0, 2, "")
    EndIf
EndFunc   ;==>_ExecuteCommand

 

Edited by Trong
#AutoIt3Wrapper_Au3Check_Parameters=-q -w 1 -w 2 -w 3 -w 4 -w 6 -w 7

Regards,
 

Link to comment
Share on other sites

  • Moderators

Is there a reason you always include all of your commented-code in your final script? Obviously the usefulness of this script would be limited to someone who is relatively new to AutoIt, so don't you think all the commented-out code with no explanation might confuse them?

"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

I just want the script does not depend on #incude
 

;~ #include <GUIConstants.au3>
;~ #include <Constants.au3>

Global $hGUI = GUICreate("AutoIt CMD", 604, 320, -1, -1)
Global $cCommand = GUICtrlCreateInput("ping google.com", 64, 10, 416, 21)
GUICtrlCreateLabel("CMD: >", 20, 15, 40, 20)
Global $cExecute = GUICtrlCreateButton("Execute Command!", 485, 8, 112, 25)
Global $cOutputBox = GUICtrlCreateEdit("", 10, 47, 578, 260, 2048 + 4 + 0x00100000 + 0x00200000 + 64) ;$ES_READONLY=2048, $ES_MULTILINE=4, $WS_HSCROLL=0x00100000, $WS_VSCROLL=0x00200000, $ES_AUTOVSCROLL=64
GUICtrlSetData(-1, "")
GUISetState(@SW_SHOW)
Global $sCommand

While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $cExecute
            $sCommand = GUICtrlRead($cCommand)
            If StringStripWS($sCommand, 8) <> "" Then
                GUICtrlSetState($cCommand, 128) ;$GUI_DISABLE=128
                GUICtrlSetState($cExecute, 128)
                GUICtrlSetData($cExecute, " eXecuting..... ")
                _ExecuteCommandStream($sCommand)
;~              GUICtrlSetData($cOutputBox, _ExecuteCommand($sCommand))
                GUICtrlSetData($cExecute, "Execute Command")
                GUICtrlSetState($cExecute, 64) ; $GUI_ENABLE=64
                GUICtrlSetState($cCommand, 64)
            EndIf
    EndSwitch
WEnd

Func _ExecuteCommandStream($sCommand)
    Local $cCommandinfo = Run('"' & @ComSpec & '" /c ' & $sCommand, @ScriptDir, @SW_HIDE, 8) ;$STDERR_MERGED=8
    Local $sOutput, $sOutputError
    While 1
        GUICtrlSetData($cOutputBox, $sOutput & @CRLF)
        $sOutput &= StdoutRead($cCommandinfo)
        If @error Then ExitLoop
        Sleep(50)
    WEnd
    GUICtrlSetData($cOutputBox, $sOutput & @CRLF)
EndFunc   ;==>_ExecuteCommandStream

Func _ExecuteCommand($sCommand)
    Local $cCommandinfo = Run('"' & @ComSpec & '" /c ' & $sCommand, @ScriptDir, @SW_HIDE, 4 + 2 + 1) ;$STDERR_CHILD=4, $STDOUT_CHILD=2, $STDIN_CHILD=1
    Local $sOutput, $sOutputError
    While 1
        $sOutput &= StdoutRead($cCommandinfo)
        If @error Then ExitLoop
    WEnd
    While 1
        $sOutputError &= StderrRead($cCommandinfo)
        If @error Then ExitLoop
    WEnd
    If $sOutput <> '' Then
        Return $sOutput
    ElseIf $sOutputError <> '' Then
        Return SetError(0, 1, $sOutputError)
    Else
        Return SetError(0, 2, "")
    EndIf
EndFunc   ;==>_ExecuteCommand

 

Edited by Trong

Regards,
 

Link to comment
Share on other sites

  • Moderators

I see where you did this in answer to a question in GH&S. I personally would not suggest starting someone new off with Magic Numbers rather than best practices - it isn't going to do them any favors as they learn to script.

"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

26 minutes ago, TheDcoder said:

I already did something like this in my Process UDF's Debug function :P.

I did not know it earlier!

I have just checked your UDF, it rather complicated.

A function to be used to a lot of other functions and up to 625 lines when compiler.

 

 

15 minutes ago, youtuber said:

@Trong There's a bug :)

1ea52ef1a9e1474293a22755dbf280eb.png

'list' is not recognized as an internal or external command,
operable program or batch file.

CMD not support OUTPUT Unicode!
OS vernacular!

Edited by Trong

Regards,
 

Link to comment
Share on other sites

1 minute ago, Trong said:

I have just checked your UDF, it rather complicated.

Try running the example, its simple enough I think :huh:.

 

TD :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

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