mikestern 0 Posted January 11 I need help on this approach : a Ping tests and tracing cmd lines needs to be run instantly without waiting for next line and the out put has to be saved on one file txt file. As test, tackled this by creating 1 txt file per line then copy all files content into sing file and finally delete those temporary files. Copying to final file does not work. See the example code. thank you in advance. expandcollapse popup#include <GUIConstantsEx.au3> #include <File.au3> #include <FileConstants.au3> $Form1 = GUICreate("Automated UAT (Project Name )", 200, 200) Global $sOutputFile = @DesktopDir & "\" & @ComputerName & "_FinalOutput.txt" Global $temp1 = @DesktopDir & "\" & "_G1.txt" Global $temp2 = @DesktopDir & "\" & "_G2.txt" Global $temp3 = @DesktopDir & "\" & "_G3.txt" $Button = GUICtrlCreateButton("START", 50, 100, 100, 33) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button $IP_Google1 = "8.8.8.8" $IP_Google2 = "8.8.4.4" $IP_Bing = "202.89.233.100" $sCommand='ping' Run(@ComSpec&' /c '&$sCommand&' '&$IP_Google1&' >> "'&$temp1&'"','',@SW_HIDE) $file1 = FileRead($temp1) FileWrite($sOutputFile,$file1) FileDelete($temp1) Run(@ComSpec&' /c '&$sCommand&' '&$IP_Google2&' >> "'&$temp2&'"','',@SW_HIDE) $file2 = FileRead($temp2) FileWrite($sOutputFile,$file1) FileDelete($temp3) Run(@ComSpec&' /c '&$sCommand&' '&$IP_Bing&' >> "'&$temp3&'"','',@SW_HIDE) $file3 = FileRead($temp2) FileWrite($sOutputFile,$file1) FileDelete($temp3) EndSwitch WEnd Share this post Link to post Share on other sites
FrancescoDiMuro 405 Posted January 11 (edited) @mikestern You need to open the file in WRITE mode, otherwise all the FileWrite() functions will fail. Adding a bit of debugging, you'll see what's going on there: expandcollapse popup#include <GUIConstantsEx.au3> #include <File.au3> #include <FileConstants.au3> $Form1 = GUICreate("Automated UAT (Project Name )", 200, 200) Global $sOutputFile = @DesktopDir & "\" & @ComputerName & "_FinalOutput.txt" Global $temp1 = @DesktopDir & "\" & "_G1.txt" Global $temp2 = @DesktopDir & "\" & "_G2.txt" Global $temp3 = @DesktopDir & "\" & "_G3.txt" $Button = GUICtrlCreateButton("START", 50, 100, 100, 33) Global $hOutputFile GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button $IP_Google1 = "8.8.8.8" $IP_Google2 = "8.8.4.4" $IP_Bing = "202.89.233.100" $sCommand='ping' Run(@ComSpec&' /c '&$sCommand&' '&$IP_Google1&' >> "'&$temp1&'"','',@SW_HIDE) $file1 = FileRead($temp1) ; Open the file in APPEND mode $hOutputFile = FileOpen($sOutputFile, $FO_APPEND) ConsoleWrite((FileWrite($hOutputFile,$file1) ? "FileWrite $file1 OK" : "FileWrite $file1 ERR") & @CRLF) FileDelete($temp1) Run(@ComSpec&' /c '&$sCommand&' '&$IP_Google2&' >> "'&$temp2&'"','',@SW_HIDE) $file2 = FileRead($temp2) ConsoleWrite((FileWrite($hOutputFile,$file1) ? "FileWrite $file2 OK" : "FileWrite $file2 ERR") & @CRLF) FileDelete($temp3) Run(@ComSpec&' /c '&$sCommand&' '&$IP_Bing&' >> "'&$temp3&'"','',@SW_HIDE) $file3 = FileRead($temp2) ConsoleWrite((FileWrite($hOutputFile,$file3) ? "FileWrite $file3 OK" : "FileWrite $file3 ERR") & @CRLF) FileDelete($temp3) ; Close the file previousely opened FileClose($hOutputFile) EndSwitch WEnd Edited January 11 by FrancescoDiMuro 1 Musashi reacted to this Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Share this post Link to post Share on other sites
mikell 1,007 Posted January 11 (edited) 7 hours ago, mikestern said: Copying to final file does not work Of course, there is not enough time left for this ... try to use RunWait instead of Run and BTW mixing the numbers in the $tempX and $fileX variables makes the thing work less too RunWait(@ComSpec&' /c '&$sCommand&' '&$IP_Google1&' >> "'&$temp1&'"','',@SW_HIDE) $file1 = FileRead($temp1) FileWrite($sOutputFile,$file1) FileDelete($temp1) RunWait(@ComSpec&' /c '&$sCommand&' '&$IP_Google2&' >> "'&$temp2&'"','',@SW_HIDE) $file2 = FileRead($temp2) FileWrite($sOutputFile,$file2) FileDelete($temp2) RunWait(@ComSpec&' /c '&$sCommand&' '&$IP_Bing&' >> "'&$temp3&'"','',@SW_HIDE) $file3 = FileRead($temp3) FileWrite($sOutputFile,$file3) FileDelete($temp3) Edit BTW(2) a much more efficient way would be like this #include <GUIConstantsEx.au3> $Form1 = GUICreate("Automated UAT (Project Name )", 200, 200) Global $sOutputFile = @DesktopDir & "\" & @ComputerName & "_FinalOutput.txt" $Button = GUICtrlCreateButton("START", 50, 100, 100, 33) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button $IP_Google1 = "8.8.8.8" $IP_Google2 = "8.8.4.4" $sCommand='ping' Local $s = "" $iPID = Run(@ComSpec & ' /c ' & $sCommand & ' ' & $IP_Google1, "", @SW_HIDE, 2) ;$STDOUT_CHILD ProcessWaitClose($iPID) $s &= StdoutRead($iPID) $iPID = Run(@ComSpec & ' /c ' & $sCommand & ' ' & $IP_Google2, "", @SW_HIDE, 2) ProcessWaitClose($iPID) $s &= StdoutRead($iPID) FileWrite($sOutputFile, $s) EndSwitch WEnd Edited January 11 by mikell 1 FrancescoDiMuro reacted to this Share this post Link to post Share on other sites
Nine 921 Posted January 11 (edited) Instead of making copies of files and deleting them and recreating them, you should do something like this : #include <GUIConstantsEx.au3> #include <File.au3> #include <FileConstants.au3> Opt("MustDeclareVars", 1) Const $IPs = ["8.8.8.8", "8.8.4.4", "202.89.233.100"] Const $sCommand = 'Ping.exe' Const $sOutputFile = @DesktopDir & "\" & @ComputerName & "_FinalOutput.txt" Local $Form1 = GUICreate("Automated UAT (Project Name )", 200, 200) Local $Button = GUICtrlCreateButton("START", 50, 100, 100, 33) GUISetState(@SW_SHOW) Local $iPID, $sText While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $Button ; FileDelete($sOutputFile) For $sIP in $IPs $iPID = Run($sCommand & ' ' & $sIP, "", @SW_HIDE, $STDERR_MERGED) ProcessWaitClose($iPID) $sText = StdoutRead($iPID) FileWrite($sOutputFile, $sText) Next MsgBox ($MB_SYSTEMMODAL,"", "Pings completed") EndSwitch WEnd ps. @mikell beat me on this one Edited January 11 by Nine Not much of a signature, but working on it... Spoiler Block all input without UAC Save/Retrieve Images to/from Text Tool to search content in au3 files Date Range Picker Sudoku Game 2020 Overlapped Named Pipe IPC x64 Bitwise Operations Fast and simple WCD IPC GIF Animation (cached) Share this post Link to post Share on other sites
mikestern 0 Posted Tuesday at 03:48 AM 18 hours ago, mikell said: Of course, there is not enough time left for this ... try to use RunWait instead of Run and BTW mixing the numbers in the $tempX and $fileX variables makes the thing work less too RunWait(@ComSpec&' /c '&$sCommand&' '&$IP_Google1&' >> "'&$temp1&'"','',@SW_HIDE) $file1 = FileRead($temp1) FileWrite($sOutputFile,$file1) FileDelete($temp1) RunWait(@ComSpec&' /c '&$sCommand&' '&$IP_Google2&' >> "'&$temp2&'"','',@SW_HIDE) $file2 = FileRead($temp2) FileWrite($sOutputFile,$file2) FileDelete($temp2) RunWait(@ComSpec&' /c '&$sCommand&' '&$IP_Bing&' >> "'&$temp3&'"','',@SW_HIDE) $file3 = FileRead($temp3) FileWrite($sOutputFile,$file3) FileDelete($temp3) Edit BTW(2) a much more efficient way would be like this #include <GUIConstantsEx.au3> $Form1 = GUICreate("Automated UAT (Project Name )", 200, 200) Global $sOutputFile = @DesktopDir & "\" & @ComputerName & "_FinalOutput.txt" $Button = GUICtrlCreateButton("START", 50, 100, 100, 33) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button $IP_Google1 = "8.8.8.8" $IP_Google2 = "8.8.4.4" $sCommand='ping' Local $s = "" $iPID = Run(@ComSpec & ' /c ' & $sCommand & ' ' & $IP_Google1, "", @SW_HIDE, 2) ;$STDOUT_CHILD ProcessWaitClose($iPID) $s &= StdoutRead($iPID) $iPID = Run(@ComSpec & ' /c ' & $sCommand & ' ' & $IP_Google2, "", @SW_HIDE, 2) ProcessWaitClose($iPID) $s &= StdoutRead($iPID) FileWrite($sOutputFile, $s) EndSwitch WEnd Thank you Mikell, I got it. that is good to know in Autoit regarding time of execution for the next instance line Share this post Link to post Share on other sites
mikestern 0 Posted Tuesday at 04:01 AM Thanks all, I understood the concept well. Nine's approach is great also , I will build the rest of Network testing remaining script parts on it. Have a great day Share this post Link to post Share on other sites