Jump to content

save content of txt file to html file


 Share

Recommended Posts

I hope someone  can help me. I am trying to import content from a text file and save it into a HTML file. So the content must be saved in the html file.
would be nice if can use html coding to give the file a nice layout with headers , table, iframes ect.
 
this is what i have tried:
 
 
#include <INet.au3> ; needed for get source (hmtl)
#include <String.au3> ; needed for stringbetween
$source = FileRead(@ScriptDir & "\psinfo.txt") ;read file
FileWrite(@ScriptDir & "\test.html",$source); save file
 
 
but the output is not as it should be, all the content is displayed on three rows instead of the two collums in the orignal txt files. Ik hope that someone can point me in the right direction.
 
All three files are attached.
Thanks in advance
 
 

test1.au3

psinfo.txt

test.html

Link to comment
Share on other sites

Here is a basic script to show how this could be done using AutoIt

$file = @ScriptDir & "\psinfo.txt"

$array = FileReadToArray($file)

$html = $array[0] & "<br><br>" & @crlf
$html &= "<table border=0 width=500>" & @crlf
For $i = 1 to UBound($array)-1
   $tmp = StringSplit($array[$i], ":")
   $html &= "<tr>" & @crlf
   $html &= "<td>" & $tmp[1] & "</td>" & @crlf
   $html &= "<td>" & StringStripWS($tmp[2], 3) & "</td>" & @crlf
   $html &= "</tr>" & @crlf
Next
$html &= "</table>" & @crlf

FileWrite(@ScriptDir & "\test.html", $html); save file

 

Edited by mikell
Link to comment
Share on other sites

The above sripts works, but only with the current content of psinfo.txt.

When i add a new line or replace the content with something else it will not generate test.html.

The same if use a whole different txt file.

Ik looks like is only working with this txt file.

 

Another stange thing is that the first row of the is displayed above the logo.

Hope that someone will help me agian

test5.au3

Link to comment
Share on other sites

The above code was only an example based on the provided .txt
In post #1 you asked for a table with columns, so :
- FileReadToArray is used to get rows
- Then on each row, StringSplit is used with colon as delimiter to get the content of the 2 columns
Of course if the file is formatted differently, you will need to manage the text differently too
Look at the comments in the code below

$file = @ScriptDir & "\psinfo.txt"

; get the array of rows
$array = FileReadToArray($file)

; declare the variable to store the html content
$html = ""

; add the content using concatenation
$html &= '<head></head><br><img style="margin-left: 30px;" src=logo-csm.png>' ;</big></td></tr></tbody></table><br style='font-family: Calibri'><big style='font-family: Calibri font-weight: bold'> </big>"
$html &= '<h2 style="width:95%; background-color:#4b8df8; margin-left: 30px; border: 0px solid #4b8df8; color:white; font-family:verdana">Scans</h2>' & @CR
$html &= '<table style="width:95%; height:50%; margin:-21px; margin-left: 30px; border: 3px solid #4b8df8;">' & @crlf

; loop through rows
For $i = 0 to UBound($array)-1
   ; split each row with colon as delimiter
   $tmp = StringSplit($array[$i], ":")
   ; build the table
   $html &= "<tr>" & @crlf   
   $html &= "<td>" & $tmp[1] & "</td>" & @crlf    ; col 1
   $html &= "<td>" & StringStripWS($tmp[2], 3) & "</td>" & @crlf   ; col 2
   $html &= "</tr>" & @crlf
Next
$html &= "</table>" & @crlf

FileWrite(@ScriptDir & "\test.html", $html); save file


 

Edited by mikell
Link to comment
Share on other sites

I am sorry, just starting with this, i thougt is would be as simple as : read a line of text from a txt-file  and write the line into html.

There are several txt files with different format, so it wil be a lot of work, for me as beginner

Or is there a way to convert the txt file to html?

 

Link to comment
Share on other sites

The link in my post will let you convert your text to html. You'd have to know how to write html to know how to convert your text to wor kthe same.

You might be able to use some _IE function to open up the webpage, insert your unformatted text, and then copy the formatted text to a file.

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