Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert a text file into another formated text file

Status
Not open for further replies.

peebman2000

Programmer
Nov 28, 2006
30
US
Hello I’m new to the forum and I’m a beginner programmer working as an intern programmer with the government. I’m researching to find code to be able to upload a text file and convert it into another formatted text file. Below is my code that I’ve coded from scratch. With limited assistance from the other developers here, I’m trying to use File Transform Engine code from to convert the text file. I have two classes, class1 which is the class for the original formatted text and I have class2 which is the class for the formatted text file I wanted to convert too. Button1 is for the upload and the conversion. When I hit button1 I get and error that says (Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\convert_text_file.txt'.) I’m trying to convert Convert_text_file.txt into dest.txt. Does anyone have any suggestion what I can do to correct this error? I would appreciate the help, thanks.

Imports System.IO
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Security
Imports System.Web.Configuration
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Text
Imports FileHelpers
Imports FileHelpers.DataLink
Imports Class1
Imports Class2


Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'when the page is loaded
Label1.Visible = False
If Not Page.IsPostBack Then

End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'put user code to initialize the page here
Label1.Visible = True
'upload file
If FileUpload1.HasFile Then
Try
FileUpload1.SaveAs(Server.MapPath("uploads\" & FileUpload1.FileName))
Catch ex As Exception
Label1.Text = "Failed because <br/>" & ex.Message
End Try
Label1.Text = "File uploaded to: <br/>" & FileUpload1.PostedFile.FileName
Else
Label1.Text = "Please select a file to open"
End If

'Dim obj As Object = New Class1
'Dim obj2 As Object = New Class2
Dim link As FileTransformEngine = New FileTransformEngine(GetType(Class1), GetType(Class2))
link.TransformFile("convert_text_file.txt", "dest.txt")

End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Label1.Visible = True
'variables
Dim filename As String = "H:\convert_text_file.txt"
Dim textline As String
textline = " "
If System.IO.File.Exists(filename) = True Then
Dim objreader As New System.IO.StreamReader(filename)
Do While objreader.Peek() <> -1
textline = textline & objreader.ReadLine() & vbNewLine
Loop
TextBox1.TextMode = TextBoxMode.MultiLine
TextBox1.Text = textline
Else
Label1.Text = "file does not exist"
End If
Label1.Text = "File has been opened"

End Sub

End Class
 
Hi,
Why do you hardcode the path of the file?
Also: On the page: , it says how you "convert" (format), using that function you are using.

Code:
  FileTransformEngine link = new FileTransformEngine(typeof(FromClass), typeof(ToClass));   
   
link.TransformFile1To2("source.txt", "dest.txt");

I recommend your government to invest in books/traningvideos, if you are to develop for them.

Olav Alexander Mjelde
Admin & Webmaster
 
When I hit button1 I get and error that says (Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\convert_text_file.txt'.)
You don't say which line causes the error, but the error is fairly self-explanatory - there isn't a file at that location. Either change the location to where the file is located or copy the file into that folder.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Okay, again I’m a beginner programmer working as a intern. I have changed my code and I'm still needing some assistance. I've looked online and in several .net books and cannot find the answer. Just to describe my code from beginning of the button1 sub, I’ve put my code below and what I’m doing is declaring my variables and then reading the text file line for line. My slnin variable is for certain text or fields that I only want to read in and stored in that variable and then I have a write out variable called slnout for text that’s stored in slnin to be written out in a different format in the slnout variable. I have an outputfile variable declared for the lines that are stored, to be an out put file. Next is when the file is uploaded, for variable fileout is the filename I want the converted text file to be called, then I want it to try to writealltext to my folder on the site. Then I also have validation messages. When I run the application, browse for the text file and press button1 to upload the file in the converted formatted text file, the application gives me a “page not found” error message and I don’t know why. I don't know what i'm doing wrong? This project is basically a test to see if can be on the development team. Can anyone look at my code and tell me what I’m doing wrong? I would appreciate it thanks.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'put user code to initialize the page here
Label1.Visible = True
'upload file
'Dim filein As String = FileUpload1.HasFile
'Dim objstreamreader As StreamReader
'objstreamreader = File.OpenText(filein)

'declaring variables
Dim slnin As String = ""
Dim slnout As String = ""
Dim outputfile As String = ""
Dim deptid As String
Dim national_id As String = ""
Dim last_name As String = ""
Dim mid_initial As String = ""
Dim address1 As String = ""
Dim first_name As String = ""
Dim city As String = ""
Dim state As String = ""
Dim postal As String = ""
Dim phone As String = ""
Dim business_title As String = ""
Dim filename As String = FileUpload1.HasFile

'upload

Dim objreader As New System.IO.StreamReader(filename)
While objreader.Peek() <> -1
slnin = objreader.ReadLine()

If Not slnin Is Nothing Then 'store text in slnin
deptid = slnin.Substring(10, 9)
national_id = slnin.Substring(20, 19)
address1 = slnin.Substring(55, 54)
first_name = slnin.Substring(30, 29)
city = slnin.Substring(30, 29)
state = slnin.Substring(6, 5)
postal = slnin.Substring(12, 11)
phone = slnin.Substring(24, 23)
business_title = slnin.Substring(30, 29)
'build line to be wrote out
slnout = deptid
slnout = national_id
slnout = address1
slnout = first_name
slnout = city
slnout = state
slnout = postal
slnout = phone
slnout = (business_title)

outputfile += slnout & vbCrLf


End If

End While
If FileUpload1.FileName Then
Dim fileout As String = "\newfile" & ".text"
Try
File.WriteAllText(Server.MapPath("uploads\" & FileUpload1.FileName) & fileout, outputfile)
Catch ex As Exception
Label1.Text = "Failed because <br/>" & ex.Message
End Try
Label1.Text = "File uploaded to server"
Else
Label1.Text = "Please select a file to upload"
End If


End Sub

End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top