Jump to content

Filereadline multiple unknown files, and recursively wrap in HTML


Recommended Posts

Hello Everyone: :bye:

I have been working on a "large project" Large for me anyways. It is in a constant state of flux and ever evolving.

Right now I'm tackling Log-file creation for reporting back to the user.

A quick example of whats going on. I have my main script (it is functional) It creates a Directory for log-files for the programs. I currently have 7 dirs, and several subdirs in some of them.

I'm using Autoit to create an HTML version of all of the logs. I have to go through each log and wrap it in HTML tags so it will render properly.

Most of the time I only have 1 log-file and I can get that wrapped and written without problem. My problem begins when I have multiple log-files(same program) in the same directory from the same clean out session.

For example with TCP optimizer, it creates a file to save the initial settings then it makes a backup of all the changes. I want to have both of these files written

to TCPoptimizerlog.html

The problem I have currently is that TCPoptimizer makes its 2nd back up using a time and date stamp down to the second if I recall! So it is difficult to correctly set the filename.

I have put together this script to serve this purpose. I currently get blank lines on my HTML that continue for at least 1000 lines. I believe I described my problem, and my thoughts behind this, So I will now post the code. If I wasn't clear enough I'm sorry. I will clarify further if it is needed.

#include <File.au3>
#include <String.au3>
#include <Array.au3>

Local $sFile = FileOpen(@DesktopDir & "\tcpoptimizerlog.html", 10)
     ; Check if file opened for reading OK
         If $sFile = -1 Then
             MsgBox(16, "Error", "Unable to open file.")
                 Exit
             EndIf

Local $path = (@HomeDrive & "\PRCS\logfiles\tcpoptimizer") ; path to the directory where multiple files are located and needed for HTML wrapping

Local $header = 'Header HTML '
Local $body = 'Body HTML'
Local $menulinks = 'Menu Links HTML'
Local $sidebar = 'Sidebar HTML'


FileWrite($sFile, $header)
FileWrite($sFile, $body)
FileWrite($sfile, $menulinks)
FileWrite($sFile, $sidebar)
FileWrite($sFile, "<h2><center>TCP Optimizer logs </center></h2>")

Local $arraytxt = FileOpen(@HomeDrive & "\PRCS\logfiles\tcpoptimizer\array.txt", 10) ; Creates a txt file for a later _FileWriteFromArray

Local $FileList = _FileListToArray($path) ;Audits the $path directory for contents results are $FileList
     If @error = 1 Then
     MsgBox(0, "", "No Folders Found.")
Exit
     EndIf
    
     If @error = 4 Then
MsgBox(0, "", "No Files Found.")
             Exit
     EndIf

        
_ArrayDisplay($FileList, "FileList") ;displays array output to window for now during the building phases will be removed down the road.

_FileWriteFromArray($arraytxt, $FileList, 0, 0, "|") ;Writes the exact filenames from the directory now in the array to the txt file.



Local $i = 2 ; starts reading the file names from the txtfile on line 2, line 1 is now my array.txt file which I don't need to wrap.

Local $read = FileReadLine($arraytxt ,$i ) ;read first file name and makes the result $read

Local $read2 = FileRead($path & "\" & $read) ;opens the directory path and the appends the exact file name(from the txtfile) for reading and subsequent HTML wrapping
    
Local $count = _FileCountLines($read2) ;counts the lines in the file open in the last function for later math needs

                
         Local $ii = 1 ;new variable for nested (Do... Until) starts on line 1

                 Do

                     FileWrite($sfile, '<li>' & $read2 & '</li>') ; recursively reads the tcpoptimizer file and wraps each line in HTML tags, then writes it to the HTML file

                             $ii = $ii + 1
                 Until $ii = $count ; the function lasts until the line count has been met



$i = $i + 1
Until $i = 3 ; makes the above (do... until) function last until it reaches the third and last filename in the Array.txt file



FileClose($path)
FileClose($read2)
FileClose($sFile)

Exit

Thank you to all that help, and to everyone that has helped me in the past as well,

Prcssupport

reason for edit: Changed title, to better reflect what\how I was doing

Edited by prcssupport
Link to comment
Share on other sites

Ok, I didn't completely understand your problem, but I give a try from what I see: :)

there is one thing that don't make sense here is this part:

Do
    FileWrite($sfile, '<li>' & $read2 & '</li>') ; recursively reads the tcpoptimizer file and wraps each line in HTML tags, then writes it to the HTML file
    $ii = $ii + 1
Until $ii = $count ; the function lasts until the line count has been met

Why do you need to do a DO UNTIL loop there? the variable $read2 is updated only once before the loop.

What are you trying to do with this loop? It seems that it is there that lies your problem?

Also, there seems to be an error with the script. The "UNTIL" at line 67 don't match any "DO".

Edited by jmon
Link to comment
Share on other sites

Hi jmon,

Thank you for pointing out the missing "do" I'm not sure if it is actually missing from my script, or if it was just missed in my copy/paste. Any ways the first do, uses $read to read the lines of text from $arraytxt. Until $i= 3 there are only 3 files in the directory and the first one I don't need. The last two are what I'm concerned about. While that function is running it nests the second (DO...until) to read the actual files and write them to html.

I hope that helps, I'm not at my desk so I can double check the "do" for now. I will check later.

Thank you

Prcssupport

Link to comment
Share on other sites

Local $i = 3 ; This variable starts reading the file names from the txtfile on line 3, line 1 is now my array.txt file which I don't need to wrap.

DO ;This is where the first do should have been.

Local $read = FileReadLine($arraytxt ,$i ) ;read first file name and makes the result $read

Local $read2 = FileRead($path & "\" & $read) ;opens the directory path and the appends the exact file name(from the array txtfile) for reading the actual file and subsequent HTML ;wrapping

Local $count = _FileCountLines($read2) ;counts the lines in the file open in the last function for later math needs


Local $ii = 1 ;new variable for nested (Do... Until) starts on line 1

Do

FileWrite($sfile, '<li>' & $read2 & '</li>') ; recursively reads the tcpoptimizer file and wraps each line in HTML tags, then writes it to the HTML file

$ii = $ii + 1
Until $ii = $count ; the function lasts until the line count has been met



$i = $i + 1
Until $i = 3 ; makes the above (do... until) function last until it reaches the third and last filename in the Array.txt file

Hi Jmon,

Thank you for your thoughts, Currently I found another way to do this. Unfortunately it requires interaction with me. But it works. Unless I can figure out this method which would be automatic(and what I prefer,) I'm going to let this thread go. I will still monitor it.

My new method is a GUI and dropping the file I want wrapped into it, and let it loop the same was as I'm doing in the above script. So far I had it working. I broke something. But I will fix it. I may possibly have to start a thread in the GUI sub forum. But we will see.

But I figured I would at least give you an answer to your questions, in hope that someone sees this(or your self) and can help me figure it out.

Thank you for pointing out the missing "DO."

I commented out the script as best as I could. To me it should have worked.

I know the actual directory $path, I don't know the actual file name to the "T" so I employed the Array to txtfile so I could read it, Change it over to the Variable $read and then add it together to create

Local $read2 = FileRead($path & "\" & $read)

I believe that should have fed me the exact path, and file name. You are right this is where my error lies. But I failed to see it so far, I tried some msgboxes to handle the errors, I wasn't able to determine what is wrong, The file opens it just fails to read. All I get is just bullets repeating down the HTML but no text.

Thank you again,

I look forward to thoughts

Prcssupport

Link to comment
Share on other sites

Hi everyone,

Ok... I lied :oops: I was going to let this die, or at least not think about this method for a while. But I couldn't stop thinking about it.

With my limited programing knowledge, I believe this is the best method to automatically do what I want. (I'm sure there is a better method out there)

I was able to get the below section of code working. I found some mistakes in my first sets of code.

I was using $vars to handle outputs hoping to make things easier. I found that sometimes the output was blank.(Causing my filereadlines to be blank) So I changed my approach.

For example my old approach:

Local $read = FileReadLine($arraytxt ,$i ); read 2nd line of $arraytxt, output to $read

$read2 = FileRead($path & "\" & $read) ; $path\$read = $read2

Local $count = _FileCountLines($read2) ;on this file $read2 should get filecountlines, but it always returned blank. I used a msgbox on $count it = nothing

This small section below is my new approach.

$read1 = FileReadline($arraytxt, $ii) ;New method ,read the 2nd line getting exact file name, outputs to $read1

$read2 = FileReadLine($path & "\" & $read1, $c) ;New method, Filereadline of the $path\$read starting at the first line

$count =_FileCountLines($path & "\" & $read1);New method, Same as above just counts the lines

The below snippet is a section I'm still having a problem with. The FileWrite continues to write past the $count(value) creating at least a thousand lines of (bullets)

Local $c, $read2

     $c = 1 ;new count is 1 starts next set of counts and logic adds 1 to the count each cycle
Do

     $read2 = FileReadLine($path & "\" & $read1, $c) ; read whole file $path\$read1
                 FileWrite($sFile, '<li>' & $read2 & '</li>') ; write html wrapper on to each line of the $path\$read1 file and puts into the HTML file... Once $count is reached                ;Filewrite should stop, But for what ever reason it continues to add (bullets)repeatedly, without anything in between the tags, for at least 1000 lines


$c = $c + 1
Until $c = $count ; the line count value for $path\$read2

I'm not sure on the logic here. This is my intent on this part of the function.

Every cycle $ii gets 1 added to it Until $ii = 10

But if there is less than 10 files to wrap (lets say 5) I want $ii = $count. But I'm not sure how to express this.

$ii = $ii + 1
Until $ii =
$count =_FileCountLines =< 10 ;Loop runs untill $ii = 10 or if less than 10 if the linecount value is less (I think I have this wrong, I'm not sure if it doesn't work for you comment out ;everything on this line above after the _FileCountLines)

Here is the full code for these functions. I left out the HTML coding that is before this. It is unimportant to the function of this section.

Local $sfile, $arraytxt, $path, $ii

$sfile = Fileopen(@homedrive & "\test\test.txt", 10) ;creates\opens a txt file on the Homedrive

$arraytxt = FileOpen(@homedrive & "\ccleaner\logs\array.txt", 0)
         if -1 Then
                 MsgBox(0, "Error 1", "The file cannot be opened")
         EndIf

$path = (@homedrive & "\ccleaner\logs\registry")

$ii = 2 ; starts read on 2nd line of file. Main Do...Until will function cyclically to the linecount or max of 10 cycles(whichever is less.)


Local $countlines1, $read1, $count


Do
$countlines1 = _FileCountLines($arraytxt) ; count the lines in $arraytxt
$read1 = FileReadline($arraytxt, $ii) ; Start on Line two in $arraytxt and output to $read1


$count =_FileCountLines($path & "\" & $read1); counts the lines in the file from the file location $path\$read1
     if @error = 1 Then
                 MsgBox(0, "Error 2", "The file cannot be opened")
     EndIf

Local $c, $read2

$c = 1 ;new count is 1 starts next set of counts and logic adds 1 to the count each cycle
Do

     $read2 = FileReadLine($path & "\" & $read1, $c) ; read whole file $path\$read1
FileWrite($sFile, '<li>' & $read2 & '</li>') ; write html wrapper on to each line of the $path\$read1 file and puts into the HTML file... ; Once $count is reached this Filewrite should stop, ;But for what ever reason it continues to add """"repeatedly, without anything in between the tags, for at least 1000 lines




$c = $c + 1
Until $c = $count ; the line count value for $path\$read2

FileWrite($sfile, "<H2><center>End of this Report</center></h2>") ;Writes an ending header to the $file


$ii = $ii + 1
Until $ii =
$count =_FileCountLines =< 10 ;Loop runs untill $ii = 10 or if less than 10 if the linecount value is less (I think I have this wrong, I'm not sure if it doesn't work for you comment out ;everything on this line above after the _FileCountLines)

FileWrite($sfile,"<h2><center>This is the end of the files</center></h2>") ; I found that this isn't being written to the $sfile) I'm not sure why.

Fileclose($sfile & $arraytxt)
Exit

I look forward to any thoughts. It has been fun the past few days getting this straightened out. I wrote all of this tonight, Almost from scratch.

Parenthood :gathering: prevented me from getting alot done on the other days. I will figure this out one way or another.

Thank you and have a good night!

Prcssupport

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...