Sign in to follow this  
Followers 0
Jacxonia

CMD in AutoIT

14 posts in this topic

Hello, I would like to include a batch file into my auto it script it should be in the same window as the auto itso cmd woul drun in the auto it gui. i Have tried it with the koda gui editor but cannt manage to get an aplication to run in the gui.

Thank you for any help!

Jacxonia

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You mean something like this?

;coded by UEZ 2011
#include <Constants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <WindowsConstants.au3>

Global Const $hGUI = GUICreate("Test", 800, 600)
Global Const $cidEdit = GUICtrlCreateEdit("", 0, 0, 800, 561, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $WS_HSCROLL, $WS_VSCROLL))
GUICtrlSetData(-1,  GetCMDVer() & @CRLF & _
                                    "Copyright © 2009 Microsoft Corporation.  All rights reserved." & @CRLF & @CRLF & @HomeDrive & @HomePath & ">")
GUICtrlSetFont(-1, 10, 600, 0, "Lucida Console")
GUICtrlSetColor(-1, 0xC0C0C0)
GUICtrlSetBkColor(-1, 0x000000)
Global Const $cidButton1 = GUICtrlCreateButton("Run Batch", 704, 568, 75, 25)
GUICtrlSetState(-1, $GUI_DISABLE)
Global Const $cidButton2 = GUICtrlCreateButton("Select Batch File", 560, 568, 99, 25)
Global Const $cidButton3 = GUICtrlCreateButton("Exit", 8, 568, 75, 25)
GUISetState(@SW_SHOW)
GUISetIcon(@SystemDir & "cmd.exe", 0, $hGUI)
Global $fBatch, $nMsg

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $cidButton3
            GUIDelete()
            Exit
        Case $cidButton1
            Run_Batch($fBatch)
        Case $cidButton2
            $fBatch = FileOpenDialog("Select a batch file to start", "", "Batch (*.bat;*.cmd)")
            If Not @error Then GUICtrlSetState($cidButton1,  $GUI_ENABLE)
    EndSwitch
WEnd

Func Run_Batch($bfile)
    Local $pid = Run(@ComSpec & ' /c "' & $bfile & '"', @ScriptDir, @SW_HIDE, $STDERR_MERGED)
    Local $line, $oldline
    _GUICtrlEdit_AppendText($cidEdit, $bfile & @CRLF)
    While Sleep(50)
        $line = StdoutRead($pid) & @LF
        If $oldline <> $line Then
            _GUICtrlEdit_AppendText($cidEdit, $line)
            $oldline = $line
        EndIf
        If Not ProcessExists($pid) Then ExitLoop
    Wend
    MsgBox(64, "Information", "Batch file has ended.", 10)
EndFunc

Func GetCMDVer()
    Local $foo = Run(@ComSpec & " /c ver", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    Local $line
    While 1
        $line &= StdoutRead($foo)
        If @error Then ExitLoop
    WEnd
    Return StringStripWS(StringStripCR($line), 3)
EndFunc   ;==>GetCMDVer

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

I think he's looking for an embedded CMD window. :unsure:

By the way, nice script UEZ!


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

I think he's looking for an embedded CMD window. :unsure:

By the way, nice script UEZ!

A "schön" German cmd Fenster example here ! Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

@UEZ not quit i would like a real CMD application in the script that can be interacted with! But that was a nice script!!! Thank you!

Appreciate any help!

Jacxonia

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Jacxonia: Sorry, I don't understand what you want exactly!

You can load a real cmd application and display it in the editbox! Maybe you can describe it better!

@wakillon: I don't think that he is searching for something like that.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

actually the way Hannes123 described it was quite acuate i search for a cmd console like when you open it in the accesories folder to be in the auto it GUI the cmd aplication should be embeded in the auto it script! The easiest way for me to explain is like this: Imagine a normal cmd window and your Auto IT Programm, not the whole cmd window should be IN the programm!

Jacxonia

Share this post


Link to post
Share on other sites

Awesome script, thanks for sharing. I was looking for something like this for a long time.

:D

Share this post


Link to post
Share on other sites

This is great.

If there is any chance that I can capture CMD output via any notification and update my edit control that would be great.

Ex:

I have a script / batch file (which takes long time to complete) and I execute from AutoIt UI with button press and its output logged in an edit control of the same UI. I should also be able to interact with UI while the script/batch file running behind.

Currently, I been using the below technique to solve my requirement.

1. Have a AutoIt GUI (with a edit control) to display results and start / stop buttons.

2. Another AutoIt Console application which actually reads the console output and updates main GUI edit box with

_GUICtrlEdit_AppendText($hEditBox, "output from cmd")

With the above technique, I could able to start / stop the process whenever I need.

I know this is not the best way to solve my problem, but this is what I came up after spending much time. Please suggest if this can be enhanced.

#include <Date.au3>
#include <GuiEdit.au3>
#include <GuiConstantsEx.au3>
; Script Start - Add your code below here
Opt("TrayMenuMode",1)
$tFile = _Date_Time_EncodeFileTime(@MON, @MDAY, @YEAR, @HOUR, @MIN, @SEC)
Local $data = ""
Local $i = 1
$hEditBox = ControlGetHandle("My Window","",150)
ControlSetText("My Window","",$hEditBox,"Start Time :: "&_Date_Time_FileTimeToStr($tFile))
While True
    $data = ConsoleRead()
If @error Then
  ExitLoop
EndIf
$sValidData = ""
If($data <> "") Then
  $aDataFetched = StringSplit($data,@CRLF)
  $i = 1
  If($aDataFetched[0] <> 1) Then
   While $i < $aDataFetched[0]
    _GUICtrlEdit_AppendText($hEditBox, @CRLF & $sValidData)
    $i +=1
   WEnd
  EndIf
EndIf
    Sleep(50)
WEnd

Share this post


Link to post
Share on other sites

Nice codes! Loving it!

You mean something like this?

;coded by UEZ 2011
#include <Constants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <WindowsConstants.au3>

Global Const $hGUI = GUICreate("Test", 800, 600)
Global Const $cidEdit = GUICtrlCreateEdit("", 0, 0, 800, 561, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $WS_HSCROLL, $WS_VSCROLL))
GUICtrlSetData(-1,  GetCMDVer() & @CRLF & _
                                    "Copyright (c) 2009 Microsoft Corporation.  All rights reserved." & @CRLF & @CRLF & @HomeDrive & @HomePath & ">")
GUICtrlSetFont(-1, 10, 600, 0, "Lucida Console")
GUICtrlSetColor(-1, 0xC0C0C0)
GUICtrlSetBkColor(-1, 0x000000)
Global Const $cidButton1 = GUICtrlCreateButton("Run Batch", 704, 568, 75, 25)
GUICtrlSetState(-1, $GUI_DISABLE)
Global Const $cidButton2 = GUICtrlCreateButton("Select Batch File", 560, 568, 99, 25)
Global Const $cidButton3 = GUICtrlCreateButton("Exit", 8, 568, 75, 25)
GUISetState(@SW_SHOW)

Global $fBatch, $nMsg

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $cidButton3
            GUIDelete()
            Exit
        Case $cidButton1
            Run_Batch($fBatch)
        Case $cidButton2
            $fBatch = FileOpenDialog("Select a batch file to start", "", "Batch (*.bat;*.cmd)")
            If Not @error Then GUICtrlSetState($cidButton1,  $GUI_ENABLE)
    EndSwitch
WEnd

Func Run_Batch($bfile)
    Local $pid = Run(@ComSpec & " /c " & $bfile, @ScriptDir, @SW_HIDE, $STDERR_MERGED)
    Local $line, $oldline
    _GUICtrlEdit_AppendText($cidEdit, $bfile & @CRLF)
    While Sleep(50)
        $line = StdoutRead($pid) & @LF
        If $oldline <> $line Then
            _GUICtrlEdit_AppendText($cidEdit, $line)
            $oldline = $line
        EndIf
        If Not ProcessExists($pid) Then ExitLoop
    Wend
    MsgBox(64, "Information", "Batch file has ended.", 10)
EndFunc

Func GetCMDVer()
    Local $foo = Run(@ComSpec & " /c ver", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    Local $line
    While 1
        $line &= StdoutRead($foo)
        If @error Then ExitLoop
    WEnd
    Return StringStripWS(StringStripCR($line), 3)
EndFunc   ;==>GetCMDVer

Br,

UEZ

Share this post


Link to post
Share on other sites

UEZ's script is amazing but when i run a bat file it says

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

All though it pop-ups a msgbox saying the bat file executed..

Am i doing something wrong ?

You mean something like this?

;coded by UEZ 2011
#include <Constants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <WindowsConstants.au3>

Global Const $hGUI = GUICreate("Test", 800, 600)
Global Const $cidEdit = GUICtrlCreateEdit("", 0, 0, 800, 561, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $WS_HSCROLL, $WS_VSCROLL))
GUICtrlSetData(-1,  GetCMDVer() & @CRLF & _
                                    "Copyright (c) 2009 Microsoft Corporation.  All rights reserved." & @CRLF & @CRLF & @HomeDrive & @HomePath & ">")
GUICtrlSetFont(-1, 10, 600, 0, "Lucida Console")
GUICtrlSetColor(-1, 0xC0C0C0)
GUICtrlSetBkColor(-1, 0x000000)
Global Const $cidButton1 = GUICtrlCreateButton("Run Batch", 704, 568, 75, 25)
GUICtrlSetState(-1, $GUI_DISABLE)
Global Const $cidButton2 = GUICtrlCreateButton("Select Batch File", 560, 568, 99, 25)
Global Const $cidButton3 = GUICtrlCreateButton("Exit", 8, 568, 75, 25)
GUISetState(@SW_SHOW)

Global $fBatch, $nMsg

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $cidButton3
            GUIDelete()
            Exit
        Case $cidButton1
            Run_Batch($fBatch)
        Case $cidButton2
            $fBatch = FileOpenDialog("Select a batch file to start", "", "Batch (*.bat;*.cmd)")
            If Not @error Then GUICtrlSetState($cidButton1,  $GUI_ENABLE)
    EndSwitch
WEnd

Func Run_Batch($bfile)
    Local $pid = Run(@ComSpec & " /c " & $bfile, @ScriptDir, @SW_HIDE, $STDERR_MERGED)
    Local $line, $oldline
    _GUICtrlEdit_AppendText($cidEdit, $bfile & @CRLF)
    While Sleep(50)
        $line = StdoutRead($pid) & @LF
        If $oldline <> $line Then
            _GUICtrlEdit_AppendText($cidEdit, $line)
            $oldline = $line
        EndIf
        If Not ProcessExists($pid) Then ExitLoop
    Wend
    MsgBox(64, "Information", "Batch file has ended.", 10)
EndFunc

Func GetCMDVer()
    Local $foo = Run(@ComSpec & " /c ver", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    Local $line
    While 1
        $line &= StdoutRead($foo)
        If @error Then ExitLoop
    WEnd
    Return StringStripWS(StringStripCR($line), 3)
EndFunc   ;==>GetCMDVer

Br,

UEZ


[font="verdana, geneva, sans-serif"] [/font]

Share this post


Link to post
Share on other sites

My guess would be that there's a space in the file name or path to the batch file.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Replace line 38 with this here:

Local $pid = Run(@ComSpec & ' /c "' & $bfile & '"', @ScriptDir, @SW_HIDE, $STDERR_MERGED)

and it should work properly.

Btw, I extended this pseudo CMD box here: (alpha version).

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

Yes that made the trick..very nice UEZ thank you...;)


[font="verdana, geneva, sans-serif"] [/font]

Share this post


Link to post
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
Sign in to follow this  
Followers 0