Jump to content
mikestern

Copy txt files content into Final txt file

Recommended Posts

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.

#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

@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:

#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 by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
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 by mikell

Share this post


Link to post
Share on other sites

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 by Nine

Share this post


Link to post
Share on other sites
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

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

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...