Jump to content

How can I merge multiple log files


Recommended Posts

I have a script that will be run on many PC's to collect info (ie. computer name, user name, domain, total memory, etc). This script creates a seperate log file for each PC. All log files will be dumped into a folder on a server. I'm trying to make a script that will import "line 2" from each PC log file into one main log file that I can later import into Excel. What is the best way to accomplish this. Also, I there could be as many as 1,000 seperate log files. Attached is an example of an individual PC log file. Below is the what I have so far for my "merge" script.

#include <Date.au3>

; Create log file

$Log_File_Dir = "logs\"

$Log_File = "PC_Info.log"

$Log_Date = StringTrimRight(_Now(), 3)

If FileExists($Log_File) Then

FileDelete($Log_File)

EndIf

FileWriteLine($Log_File, "PC-Info Log: Complied on " & $Log_Date)

FileWriteLine($Log_File, " ")

FileWriteLine($Log_File, "Computer Name" & "," & "Login Domain" & "," & "LAN IP-Address 1" & "," & "User Name" & "," & "Operating System" & "," & "OS Service Pack" & "," & "Processor Speed" & "," & "Total RAM" & "," & "Hard Drive 1 Total Space" & "," & "Log Date-Time")

I want to add the import line at the end of the above code and I want it to repeat for all log files in the logs folder. Thanks in advance for your help.

Link to comment
Share on other sites

give this a try.

#include <File.au3>
$Log_File = "logs\"
dim $Lines[1] = [0]
$files = _FileListToArray( $Log_File , "*PC_Info.log" , 1 )
If (Not @error) and (IsArray( $files )) Then
    Dim $Lines[$files[0] +1]; Will store all the Lines in this array
    $Lines[0] = $files[0]
    For $a = 1 to $files[0]
        $Lines[$a] = FileReadLine( $files[$a] , 2 )
    Next
EndIf
;Do whatever you want with $Lines.
Edited by rbhkamal

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

Thank you both for your help. Below is what I came up with for my final code. It seems to work well.

#include <File.au3>

#include <Date.au3>

$Log_File_Dir = "logs\"

$Log_File = "PC_Info.log"

$Log_Date = StringTrimRight(_Now(), 3)

; Prepare the main log file

If FileExists($Log_File) Then

FileDelete($Log_File)

EndIf

FileWriteLine($Log_File, "PC-Info Log: Complied on " & $Log_Date)

FileWriteLine($Log_File, " ")

FileWriteLine($Log_File, "Computer Name" & "," & "Login Domain" & "," & "LAN IP-Address 1" & "," & "User Name" & "," & "Operating System" & "," & "OS Service Pack" & "," & "Processor Speed" & "," & "Total RAM" & "," & "Hard Drive 1 Total Space" & "," & "Log Date-Time")

; Merge data from all log files into the main log file

$files = _FileListToArray( $Log_File_Dir , "*.log" , 1 )

If (Not @error) and (IsArray( $files )) Then

For $a = 1 to $files[0]

$Line = FileReadLine( $Log_File_Dir & $files[$a] , 2 )

FileWriteLine($Log_File, $Line)

Next

EndIf

Thanks again

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...