mikestern Posted January 11, 2021 Share Posted January 11, 2021 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 Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted January 11, 2021 Share Posted January 11, 2021 (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, 2021 by FrancescoDiMuro Musashi 1 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
mikell Posted January 11, 2021 Share Posted January 11, 2021 (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, 2021 by mikell FrancescoDiMuro 1 Link to comment Share on other sites More sharing options...
Nine Posted January 11, 2021 Share Posted January 11, 2021 (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, 2021 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
mikestern Posted January 12, 2021 Author Share Posted January 12, 2021 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 Link to comment Share on other sites More sharing options...
mikestern Posted January 12, 2021 Author Share Posted January 12, 2021 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 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