caramen Posted September 16, 2019 Share Posted September 16, 2019 (edited) Hello peoples. #include <Constants.au3> Global $DOS, $Message = '' ;; added "= ''" for show only. ;~ $DOS = Run(@ComSpec & " /k Ping www.google.com", "", @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD) $DOS = Run(@ComSpec & " /k " & "\\PRET\ServeurFM\0.bat", "", @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD ) ProcessWaitClose($DOS) $Message = StdoutRead($DOS) MsgBox(0, "Stdout Read:", $Message) I'm actually managing an apach mysql server. Using this snippet I do what I want. Run a batch and log everything into a TXT file. Two things needed : -I need to run 0.bat into c\server\0.bat -I need to see the batch and keep ability to type inside it. Thaaaaaaaanks fo help ! Edited September 16, 2019 by caramen My video tutorials : ( In construction ) || My Discord : https://discord.gg/S9AnwHw How to Ask Help || UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote Spoiler Water's UDFs:Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki Link to comment Share on other sites More sharing options...
mistersquirrle Posted September 16, 2019 Share Posted September 16, 2019 It's not too clear to me for each point what you're looking to do. Is your file already located in C:\server? Then shouldn't you just be able to use that path (C:\server\0.bat)? If you're looking to move it to that folder, then would FileMove would be what you're looking for, correct? Then maybe FileExists to verify, before a Run with the C path. Are you talking about having an AutoIt GUI open the file, and allow you to edit it, and then save it back to the file? I made some changes to your code, which would allow AutoIt to do other things while the bat/ cmd is running: #include <Constants.au3> Global $DOS, $Message = '' ;; added "= ''" for show only. $DOS = Run(@ComSpec & " /k Ping www.google.com", "", @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD) ; Could also use $STDERR_MERGED ;~ $DOS = Run(@ComSpec & " /k " & "\\PRET\ServeurFM\0.bat", "", @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD ) ; Could also use $STDERR_MERGED ;~ ProcessWaitClose($DOS) ConsoleWrite("> Stdout Read: " & @CRLF) While 1 $Message = StdoutRead($DOS) If @error Then ; Maybe do something? Most likely the cmd has finished though ExitLoop EndIf If $Message <> '' Then ConsoleWrite(@TAB & $Message) ; May want to add a '& @CRLF' here. For Ping, it's not necessisary $Message = '' EndIf Sleep(10) ; Potentially multiple lines can come in if you sleep too long, or do a lot of other things between StdoutRead checks WEnd ConsoleWrite("> Stdout Done " & $Message & @CRLF) If you can provide more information on the functionality that you're looking for, we can probably give you some better information We ought not to misbehave, but we should look as though we could. Link to comment Share on other sites More sharing options...
caramen Posted September 17, 2019 Author Share Posted September 17, 2019 It's really simple fonctionnality, but I can see this issue occur to a lot of people into the forum and there is never an answer to do it. Each time the post is dying or OP find another solution. The complicated thing is, to stream and log at the same time into the CMD console. -I want to run a .bat file. (Automated or not it doesn't matter.) -I need to interact with the bat file for monitoring purpose. Be able to see if I have errors popping in the console. -I can do c:/server/bat.bat OR \\PRET\server\bat.bat it doesn't matter to. I got both access on the server. -I want to log everything for further use. I read a lot of post about this I can't figure out a solution to point me in the right direction. My video tutorials : ( In construction ) || My Discord : https://discord.gg/S9AnwHw How to Ask Help || UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote Spoiler Water's UDFs:Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki Link to comment Share on other sites More sharing options...
mistersquirrle Posted September 17, 2019 Share Posted September 17, 2019 Something like this? expandcollapse popup#include <Constants.au3> #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <FontConstants.au3> #include <File.au3> #include <GuiEdit.au3> OnAutoItExitRegister("_Exit") #Region ### START Koda GUI section ### Form= ; [0] - width, [1] - height, [2] - left, [3] - top Local $iGuiRes[4] = [588, 440] $iGuiRes[2] = (@DesktopWidth / 2) - ($iGuiRes[0] / 2) $iGuiRes[3] = (@DesktopHeight / 2) - ($iGuiRes[1] / 2) $hGui = GUICreate("Batch tester", $iGuiRes[0], $iGuiRes[1], $iGuiRes[2], $iGuiRes[3]) $cFilePath = GUICtrlCreateInput("0.bat", 8, 8, 320, 25) GUICtrlSetFont(-1, 12.5) $cStart = GUICtrlCreateButton("Launch bat", 336, 8, 94, 25) GUICtrlSetFont(-1, 12) $cStop = GUICtrlCreateButton("Stop bat", 438, 8, 94, 25) GUICtrlSetFont(-1, 12) Local $iEditWidth = $iGuiRes[0] / 2 - 16 GUICtrlCreateLabel("Batch file output", 16, 48, $iEditWidth - (8 * 3), 17) GUICtrlSetFont(-1, 9, $FW_BOLD) $cBatOutput = GUICtrlCreateEdit("", 8, 72, $iEditWidth, 353, BitOR($WS_VSCROLL, $ES_AUTOVSCROLL, $ES_READONLY)) _GUICtrlEdit_SetReadOnly(ControlGetHandle($hGui, '', $cBatOutput), True) GUICtrlSetData(-1, "") GUICtrlCreateLabel("Batch file contents", $iEditWidth + (8 * 5), 48, $iEditWidth - (8 * 3) - 58, 17) GUICtrlSetFont(-1, 9, $FW_BOLD) $cSave = GUICtrlCreateButton("Save", ($iEditWidth * 2) - (8 * 6), 45, $iEditWidth - ($iEditWidth - (8 * 3) - 50), 25) GUICtrlSetFont(-1, 10) $cBatEditor = GUICtrlCreateEdit("", $iEditWidth + (8 * 3), 72, $iEditWidth, 353) GUICtrlSetData(-1, "") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Local $hDOS = False, $sFilePath, $hFile, $sPrevFileText, $sGuiText, $Message While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $cStart ; Can't start the batch if the batch is already running If $hDOS And ProcessExists($hDOS) Then ContinueCase $sFilePath = GUICtrlRead($cFilePath) $hDOS = _RunBat($sFilePath) If @error Then GUICtrlSetData($cBatOutput, $hDOS & ' - ' & @error) $hDOS = False Else ConsoleWrite("Batch file contents: " & FileRead($sFilePath) & @CRLF) GUICtrlSetData($cBatEditor, FileRead($sFilePath)) GUICtrlSetData($cBatOutput, '') EndIf Case $cSave $sGuiText = GUICtrlRead($cBatEditor) ; Make sure that we're not just writing a blank file, and that there's actually something to change If $sGuiText <> '' And $sGuiText <> $sPrevFileText Then ConsoleWrite("Current BatEditor text:" & @CRLF & $sGuiText & @CRLF) $hFile = FileOpen($sFilePath, 10) If $hFile <> -1 Then ConsoleWrite("Batch file handle: " & $hFile & @CRLF) FileWrite($hFile, $sGuiText) FileFlush($hFile) FileClose($hFile) $sPrevFileText = $sGuiText EndIf EndIf Case $cStop ; Only need to stop something, if something exists If $hDOS And ProcessExists($hDOS) Then $Message = StdoutRead($hDOS) ProcessClose($hDOS) $Message &= @CRLF & " Output stopped" & @CRLF & $hDOS $hDOS = False _GUICtrlEdit_AppendText($cBatOutput, @CRLF & $Message) $Message = '' ; Write the output to a log file $hFile = FileOpen("output.log", 10) If $hFile <> -1 Then FileWrite($hFile, GUICtrlRead($cBatOutput)) FileFlush($hFile) FileClose($hFile) EndIf EndIf EndSwitch If $hDOS And ProcessExists($hDOS) Then $Message = StdoutRead($hDOS) If @error Then $Message &= @CRLF & " Output stopped" & @CRLF & $hDOS $hDOS = False EndIf If $Message <> '' Then ConsoleWrite($Message & @CRLF) _GUICtrlEdit_AppendText($cBatOutput, @CRLF & $Message) $Message = '' EndIf EndIf WEnd Func _RunBat($sFilePath = '0.bat') $sFilePath = StringReplace($sFilePath, '"', '') $sFilePath = StringReplace($sFilePath, "'", '') If Not FileExists($sFilePath) Then Return SetError(1, 0, "File doesn't exist") Local $sFullPath = _PathFull($sFilePath) If Not FileExists($sFullPath) Then Return SetError(2, 0, "File (PathFull) doesn't exist") ConsoleWrite($sFullPath & @CRLF) Return Run(@ComSpec & ' /k "' & $sFullPath & '"', "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) ;~ Return Run(@ComSpec & " /k Ping www.google.com -t", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) EndFunc ;==>_RunBat Func _Exit() $sGuiText = GUICtrlRead($cBatEditor) If $sGuiText <> '' And $sGuiText <> $sPrevFileText Then If Not IsHWnd($hFile) Then $hFile = FileOpen($sFilePath, 10) If $hFile <> -1 Then FileWrite($hFile, $sGuiText) FileClose($hFile) EndIf EndIf If $hDOS Then ProcessClose($hDOS) Exit EndFunc ;==>_Exit This gives you the ability to load up one .bat file, and both display its output and allows you to edit the batch file directly from the same window. It's a pretty basic idea, and probably not the best handling of the file (make a backup). But I'm still not quite sure what your goal is. I want to run a .bat file. (Automated or not it doesn't matter.) Check I need to interact with the bat file for monitoring purpose. Be able to see if I have errors popping in the console. Not sure what you mean by 'interact' with it, but otherwise you can see the output, so check? I can do c:/server/bat.bat OR \\PRET\server\bat.bat it doesn't matter to. I got both access on the server. Check I want to log everything for further use. Check, an 'output.log' file is created when you stop the bat file (it will be overwritten by the next stop) Musashi and Earthshine 2 We ought not to misbehave, but we should look as though we could. Link to comment Share on other sites More sharing options...
Musashi Posted September 17, 2019 Share Posted September 17, 2019 On 9/16/2019 at 1:15 PM, caramen said: -I need to see the batch and keep ability to type inside it. 1 hour ago, mistersquirrle said: Not sure what you mean by 'interact' with it @mistersquirrle : BTW, nice little script. I suppose @caramen wants to make inputs and display the results in the STDOUT stream while the batch file is still running. Example (test.cmd) : @echo off :START echo. echo Select : echo [1] = dir *.* echo [2] = net user echo [3] = ipconfig echo [4] = QUIT echo. set Modus=0 set /p modus="Select a number : " if %modus%==1 goto SELECTION1 if %modus%==2 goto SELECTION2 if %modus%==3 goto SELECTION3 goto END :SELECTION1 echo SELECTION 1 : dir *.* goto START :SELECTION2 echo SELECTION 2 : net user goto START :SELECTION3 echo SELECTION 3 : ipconfig goto START :END Just a guess and not a solution, of course. I'm not even sure if this is possible. "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
Earthshine Posted September 17, 2019 Share Posted September 17, 2019 (edited) yeah, there is not much interacting with a batch file execution. once started you can't do anything. perhaps the OP can explain what it all means... the bat could output a txt file which can be monitored.. that's about as good as it gets there is no changing stuff while it's running. Edited September 17, 2019 by Earthshine My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
mistersquirrle Posted September 17, 2019 Share Posted September 17, 2019 (edited) 52 minutes ago, Musashi said: Example (test.cmd) : @echo off :START echo. echo Select : echo [1] = dir *.* echo [2] = net user echo [3] = ipconfig echo [4] = QUIT echo. set Modus=0 set /p modus="Select a number : " if %modus%==1 goto SELECTION1 if %modus%==2 goto SELECTION2 if %modus%==3 goto SELECTION3 goto END :SELECTION1 echo SELECTION 1 : dir *.* goto START :SELECTION2 echo SELECTION 2 : net user goto START :SELECTION3 echo SELECTION 3 : ipconfig goto START :END Just a guess and not a solution, of course. I'm not even sure if this is possible. I'll be completely honest, I've never really used batch files or done much with CMD in general. This is a good little test batch file, and this actually does illustrate 'interacting' with the file, so thanks! Going off that I have a solution for that. Here's the updated script: expandcollapse popup;https://www.autoitscript.com/forum/topic/200293-cmd-output-logged/?tab=comments#comment-1436999 #include <Constants.au3> #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <FontConstants.au3> #include <File.au3> #include <GuiEdit.au3> OnAutoItExitRegister("_Exit") #Region ### START Koda GUI section ### Form= ; [0] - width, [1] - height, [2] - left, [3] - top Local $iGuiRes[4] = [1024, 466] $iGuiRes[2] = (@DesktopWidth / 2) - ($iGuiRes[0] / 2) $iGuiRes[3] = (@DesktopHeight / 2) - ($iGuiRes[1] / 2) $hGui = GUICreate("Batch tester", $iGuiRes[0], $iGuiRes[1], $iGuiRes[2], $iGuiRes[3]) $idFilePath = GUICtrlCreateInput("0.bat", 8, 8, 320, 25) GUICtrlSetFont(-1, 12.5) $idStart = GUICtrlCreateButton("Launch bat", 336, 8, 94, 25) GUICtrlSetFont(-1, 12) $idStop = GUICtrlCreateButton("Stop bat", 438, 8, 94, 25) GUICtrlSetFont(-1, 12) Local $iEditWidth = $iGuiRes[0] / 2 - 16 GUICtrlCreateLabel("Batch file output", 16, 48, $iEditWidth - (8 * 3), 17) GUICtrlSetFont(-1, 9, $FW_BOLD) $idBatOutput = GUICtrlCreateEdit("", 8, 72, $iEditWidth, 353, BitOR($WS_VSCROLL, $ES_AUTOVSCROLL, $ES_READONLY)) GUICtrlSetFont(-1, 10, $FW_NORMAL, $GUI_FONTNORMAL, 'Consolas', $CLEARTYPE_QUALITY) ;~ _GUICtrlEdit_SetReadOnly(ControlGetHandle($hGui, '', $idBatOutput), True) GUICtrlSetData(-1, "") GUICtrlCreateLabel("Batch file contents", $iEditWidth + (8 * 5), 48, $iEditWidth - (8 * 3) - 58, 17) GUICtrlSetFont(-1, 9, $FW_BOLD) $idSave = GUICtrlCreateButton("Save", ($iEditWidth * 2) - (8 * 6), 45, $iEditWidth - ($iEditWidth - (8 * 3) - 50), 25) GUICtrlSetFont(-1, 10) $idBatEditor = GUICtrlCreateEdit("", $iEditWidth + (8 * 3), 72, $iEditWidth, 353) GUICtrlSetFont(-1, 10, $FW_NORMAL, $GUI_FONTNORMAL, 'Consolas', $CLEARTYPE_QUALITY) GUICtrlSetData(-1, "") $idStdinWriteInput = GUICtrlCreateInput("", 8, 435, 320, 25) GUICtrlSetFont(-1, 12.5) $idStdinWriteSend = GUICtrlCreateButton("StdinWrite", 336, 435, 120, 25) GUICtrlSetFont(-1, 12) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Local $hDOS = False, $sFilePath, $hFile, $sPrevFileText, $sGuiText, $Message, $sStdinWrite, $bEnded = True While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $idStart ; Can't start the batch if the batch is already running If $hDOS And ProcessExists($hDOS) Then ContinueCase $sFilePath = GUICtrlRead($idFilePath) $hDOS = _RunBat($sFilePath) If @error Then GUICtrlSetData($idBatOutput, $hDOS & ' - ' & @error) $hDOS = False Else ConsoleWrite("Batch file contents: " & FileRead($sFilePath) & @CRLF) GUICtrlSetData($idBatEditor, FileRead($sFilePath)) GUICtrlSetData($idBatOutput, '') $bEnded = True EndIf Case $idSave $sGuiText = GUICtrlRead($idBatEditor) ; Make sure that we're not just writing a blank file, and that there's actually something to change If $sGuiText <> '' And $sGuiText <> $sPrevFileText Then ConsoleWrite("Current BatEditor text:" & @CRLF & $sGuiText & @CRLF) $hFile = FileOpen($sFilePath, 10) If $hFile <> -1 Then ConsoleWrite("Batch file handle: " & $hFile & @CRLF) FileWrite($hFile, $sGuiText) FileFlush($hFile) FileClose($hFile) $sPrevFileText = $sGuiText EndIf EndIf Case $idStop ; Only need to stop something, if something exists If $hDOS And ProcessExists($hDOS) Then $Message = StdoutRead($hDOS) StdioClose($hDOS) ProcessClose($hDOS) $Message &= " Output stopped" & @CRLF & $hDOS $hDOS = False _GUICtrlEdit_AppendText($idBatOutput, @CRLF & $Message) $Message = '' ; Write the output to a log file $hFile = FileOpen("output.log", 10) If $hFile <> -1 Then FileWrite($hFile, GUICtrlRead($idBatOutput)) FileFlush($hFile) FileClose($hFile) EndIf EndIf Case $idStdinWriteSend ; Send data to the cmd $sStdinWrite = GUICtrlRead($idStdinWriteInput) If $sStdinWrite <> '' And $hDOS And ProcessExists($hDOS) Then $Message = '> ' & $sStdinWrite _GUICtrlEdit_AppendText($idBatOutput, @CRLF & $Message & @CRLF) StdinWrite($hDOS, $sStdinWrite) EndIf EndSwitch If $hDOS And ProcessExists($hDOS) Then $Message = StdoutRead($hDOS) If @error Then $Message &= @CRLF & " Output stopped" & @CRLF & $hDOS $hDOS = False $bEnded = True EndIf If $Message <> '' Then ConsoleWrite($Message) _GUICtrlEdit_AppendText($idBatOutput, $Message) $Message = '' EndIf ElseIf $bEnded = False Then ; In this case the process was closed but we didn't record it already $Message = StdoutRead($hDOS) $Message &= @CRLF & " Output stopped" & @CRLF & $hDOS If $hDOS Then ; Just make sure it's closed StdioClose($hDOS) ProcessClose($hDOS) EndIf $hDOS = False If $Message <> '' Then ConsoleWrite($Message) _GUICtrlEdit_AppendText($idBatOutput, $Message) $Message = '' EndIf EndIf WEnd Func _RunBat($sFilePath = '0.bat') $sFilePath = StringReplace($sFilePath, '"', '') $sFilePath = StringReplace($sFilePath, "'", '') If Not FileExists($sFilePath) Then Return SetError(1, 0, "File doesn't exist") Local $sFullPath = _PathFull($sFilePath) If Not FileExists($sFullPath) Then Return SetError(2, 0, "File (PathFull) doesn't exist") ConsoleWrite($sFullPath & @CRLF) Return Run(@ComSpec & ' /c "' & $sFullPath & '"', "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD + $STDIN_CHILD) ; Important are the flags, we have Err (unused), Out (output) and In (sending input) ;~ Return Run(@ComSpec & " /k Ping www.google.com -t", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) EndFunc ;==>_RunBat Func _Exit() $sGuiText = GUICtrlRead($idBatEditor) If $sGuiText <> '' And $sGuiText <> $sPrevFileText Then If Not IsHWnd($hFile) Then $hFile = FileOpen($sFilePath, 10) If $hFile <> -1 Then FileWrite($hFile, $sGuiText) FileClose($hFile) EndIf EndIf If $hDOS Then StdioClose($hDOS) ProcessClose($hDOS) EndIf Exit EndFunc ;==>_Exit I added the ability to send *something* to the cmd, and it looks like that it works just dandy for me. I also made some layout tweaks, larger window, some styling on the output, etc. Hopefully @caramen can let us know if this is what they're looking for. Edited September 17, 2019 by mistersquirrle small script update We ought not to misbehave, but we should look as though we could. Link to comment Share on other sites More sharing options...
Musashi Posted September 17, 2019 Share Posted September 17, 2019 (edited) 3 hours ago, mistersquirrle said: I added the ability to send *something* to the cmd, and it looks like that it works just dandy for me. Definitely an improvement ! The SciTE console contains the entire output if you do multiple selections. Edit controls like $idBatOutput are limited by default, so check the actual setting with _GUICtrlEdit_GetLimitText(). When the limit is reached, no further output will be displayed by _GuiCtrlEdit_AppendText(). You can increase the value with for example _GUICtrlEdit_SetLimitText($idBatOutput, xxxxxx) - but not endlessly 🤔. 3 hours ago, mistersquirrle said: Hopefully @caramen can let us know if this is what they're looking for. Yes, so don't spend too much time on this topic until we know, where @caramen 's journey will go . Edited September 17, 2019 by Musashi "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
rudi Posted September 23, 2019 Share Posted September 23, 2019 Hi @caramen What I didn't really get (propably missed it), is, why do you want to "operate-BAT-from-Autoit"? Why not code, what you need in autoit directly? I've use CMD files quite a lot as well, but most of tasks, I've done in CMD files before, are now AU3 coded: Here I have much more flexibility... Regards, Rudi. Earth is flat, pigs can fly, and Nuclear Power is SAFE! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now