TXT to HTML translator Learn programming Visual Basic (VB.net)



Lesson:

File Management


Exercise:

TXT to HTML translator


Objetive:

Create a "Text to HTML converter", which will read a source text file and create a HTML file from its contents. For example, if the file contains:

Hola
Soy yo
Ya he terminado

The name of the destination file must be the same as the source file, but with ".html" extension (which will replace the original ".txt" extension, if it exists). The "title" in the "head" must be taken from the file name.


Code:

Imports System
Imports System.IO
Namespace TXTtoHTML
    Class Program
        Private Shared Sub Main()
            Console.Write("Enter name of file: ")
            Dim nameFileTxt As String = Console.ReadLine()
            Dim nameFileHtml As String = nameFileTxt.Substring(0, nameFileTxt.Length - 4)

            If File.Exists(nameFileTxt) Then
                Dim myfileTxt As StreamReader
                Dim myfileHtml As StreamWriter
                myfileTxt = File.OpenText(nameFileTxt)
                myfileHtml = File.CreateText(nameFileHtml & ".html")
                Dim line As String
                myfileHtml.WriteLine("")
                myfileHtml.WriteLine("")
                myfileHtml.WriteLine("" & nameFileHtml & "")
                myfileHtml.WriteLine("")
                myfileHtml.WriteLine("")

                Do
                    line = myfileTxt.ReadLine()
                    If line IsNot Nothing Then myfileHtml.WriteLine("" & line & "")
                Loop While line IsNot Nothing

                myfileHtml.WriteLine("")
                myfileHtml.WriteLine("")
                myfileTxt.Close()
                myfileHtml.Close()
            End If
        End Sub
    End Class
End Namespace



Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.