Jump to content
Sign in to follow this  
Darktan

Problems parsing a series of log files

Recommended Posts

Darktan

So I'm working on a small project for work. I have a script that runs on several servers to pull some very odd information. The local script pulls the information and copies it (or overwrites it) to a file on the local machine. It then copies it to a central server share. The file is a simple one line text file with comma delimiters and with the CSV extension. There's a reason for this but this part works fine.

The problem I have is with the other script. This script is supposed to parse the directory and read the first line of each file and combine them all into one file. What I am actually doing is using the outputted file to be loaded into Excel and altered slightly. Just a little formatting and then turned into a read-only Excel sheet for the other techs here. Very simple I thought and I thought I had it done but for some reason it's not working. I get blank lines or old information.

I've actually tried to rewrite this several times (as there's 500 ways to do something wrong and 20 ways to do it right). The first code is sort of the original with a few adjustments. I added a nested loop thinking it would solve the issue. It didn't.

#include <Array.au3>
#include <file.au3>
#include <Excel.au3>

Dim $aRecords[1];Declare Array

$search = FileFindFirstFile("z:\*.*") ;Search pattern

; Check if the search was successful
If $search = -1 Then
    MsgBox(4096,"","ERROR: No files/directories matched the search pattern"); Code for logging
    Exit
EndIf

While 1
    $MARK = FileFindNextFile($search)
    If @error Then ExitLoop
    While 1
        $file = FileOpen($MARK,0)
        If @error Then ExitLoop
        $line = FileReadLine($file,1)
        MsgBox(4096, "File:", $MARK & @CRLF & $line)
        _ArrayAdd($aRecords,$line)
        WEnd
    FileClose($file)
    
WEnd

; Close the search handle
FileClose($search)

_ArrayDisplay($aRecords, "$avArray set manually 1D");Show me the array list for debugging

_FileWriteFromArray("C:\AutoIt Scripts\SrvInfo\SrvTemp.csv", $aRecords, 1);Write Array to temp file. Skip first record as it's the array size and isn't needed

This next code is better IMO and taken from a script I found here. It's much cleaner but it still doesn't work.

#include <Array.au3>
#include <File.au3>
Dim $Folder = 'Z:'
Dim $Files = '*.csv'
Dim $FileArray = _FileListToArray($Folder, $Files)
Dim $aRecords;Declare Array

For $i = 0 To UBound($FileArray-1)
    ReadFiles($FileArray[$i])
Next

Func ReadFiles($FileToOpen)
    $FileOpen = FileOpen($FileToOpen, 0)
    $line = FileReadLine($FileOpen,1)
    MsgBox(0,$FileToOpen,$line)
    FileClose($FileOpen)
    _ArrayDisplay($FileArray,"Files")
    _ArrayDisplay($aRecords,"Records")
EndFunc;==>ReadFiles

I think it might be I'm getting errors opening the files, but there shouldn't be an issue. The files are not in use by anything else. I ran it on a series of backup AutoIt files (*.bak) in a different directory and it seemed to work. Moving the files off the share and now local, it still comes up empty for the files. Even if I create a new text file it also is not getting picked up.

Does anyone see anything wrong with the code? Or where I could put some debug lines to try and track down if this is a file access issue? Any suggestions are welcome.

Peace

Darktan

Share this post


Link to post
Share on other sites
PsaltyDS

Pass the full path to the file:

For $i = 0 To UBound($FileArray - 1)
    ReadFiles($Folder & "\" & $FileArray[$i])
Next

:D


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Darktan

Pass the full path to the file:

For $i = 0 To UBound($FileArray - 1)
    ReadFiles($Folder & "\" & $FileArray[$i])
Next

:D

I thank you Psalty. That was it. I believe you might've helped me out with another issue in the past too so thanks for that as well if I didn't mention it.

I do feel better. I knew the basic code looked right and it had to be a small issue or something easily overlooked. Sometimes you need a second pair of eyes. Or possibly some more sleep...

Peace

Darktan

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.