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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write all text to server folder under new file name 1

Status
Not open for further replies.

peebman2000

Programmer
Joined
Nov 28, 2006
Messages
30
Location
US
Hello again, again I’m a newbie and I’m still working on the same project and now I have a deadline to meet of December 18th. Below is my code, I’m trying select a text file from a directory and convert it into a different formatted text file and uploaded it to the server in a different text file name. Just to give you an explanation of my code, under button1 I have declared my variables of what fields of data I want from the selected text file. Then if the fileupload has the file I selected I want it to open the text file and then read each line, then I want it to pull out the characters of data I only want from that text file. Then I want it to write all of the text that I only want from the text file to a folder on the server under the new file name of “newfile.txt”. For some reason it will not write the new text out nor will it upload it to the server folder under newfile.txt. Does anyone know why? I know its something small, but I can’t see it, can someone please show me what I’m missing. Any additional code would be helpful also. I appreciate it thank you.

Imports System.IO
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.Security
Imports System.Web.Configuration
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Text
Imports System.Web.Management
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
Dim slnin As String = ""
Dim slnout As String = ""
Dim outputfile As String = ""
'Dim deptid As String
Dim semi 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 hire_dt As String = ""
Dim business_title As String = ""
Dim space As String = ""
space = " "
semi = ";"

If FileUpload1.HasFile Then

'upload file
'Dim filein As String = FileUpload1.HasFile
'Dim objstreamreader As StreamReader
'

'declaring variables

'Dim orig_hire_dte As String = ""
'im filename As String
'Dim objstreamreader As StreamReader
Dim filename As String = ""
If filename = String.Empty Then
filename = FileUpload1.PostedFile.FileName
End If
Dim sr As StreamReader
'Dim streamreader As New StreamReader("c:\mastertestfile.txt")
sr = File.OpenText(filename)

While sr.Peek() <> -1
slnin = sr.ReadLine

'slnin = objstreamreader.ReadLine
'Do While Not strline Is Nothing

'objstreamreader = File.OpenText(filename) 'may undo
'objstreamreader = File.OpenText()'may undo

If Not slnin Is Nothing Then 'store text in slnin
'deptid = slnin.Substring(3000, 1000)
national_id = slnin.Substring(1384, 20)
last_name = slnin.Substring(1650, 30)
mid_initial = slnin.Substring(1720, 8)
first_name = slnin.Substring(1685, 15)
address1 = slnin.Substring(1193, 25)
city = slnin.Substring(1305, 13)
state = slnin.Substring(1365, 3)
postal = slnin.Substring(1372, 11)
phone = slnin.Substring(548, 23)
hire_dt = slnin.Substring(188, 21)
business_title = slnin.Substring(33, 18)

'last_name = last_name.PadRight(25)
'build line to be wrote out
'slnout = deptid
slnout = national_id + semi
slnout += last_name + semi
slnout += mid_initial + semi
slnout += first_name + semi
slnout += address1 + semi
slnout += city + semi
slnout += state + semi
slnout += postal + semi
slnout += phone + semi
slnout += hire_dt + semi
slnout += business_title + semi

outputfile += slnout & vbCrLf

End If
End While
'Response.Write(slnout)
'Response.End()

Dim fileout As String = "\newfile" & ".txt"
Try '& FileUpload1.Filename)
File.WriteAllText(Server.MapPath("uploads\") & fileout, outputfile)
Label1.Text = "File uploaded to server" '& FileUpload1.PostedFile.FileName
Catch ex As Exception
''FileUpload1.SaveAs(Server.MapPath("uploads\" & FileUpload1.FileName))
Label1.Text = "Failed because <br/>" & ex.Message
End Try
sr.Close()
'objstreamreader.Close()
Console.ReadLine()
Else
Label1.Visible = True
Label1.Text = "Please select a file to upload"

End If
End Sub
End Class
 
Try stepping through the code and seeing which lines are executed. Debugging is the easiest method to find out why something isn't working.

Also, you should really look into using a StringBuilder rather than a String for creating your new file.


____________________________________________________________

Need help finding an answer?

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

 
Thanks ca8msm, I tried using the stringbuilder; like I said i'm a beginner programming, I had little trouble coding. I starting researching more on stringbuilding but then I got focused on just getting it to write out. If you have some sites or code that kinds of give me a better idea on how to code the stringbuilder I would appreciate it. Once I get it to write out then I'll back and do the strignbuilder for my spaces. Thanks.
 
Have a look at the help files if you have trouble with any part of the framework as there is a file for each class, method and property:



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Thanks ca8msm, I looked at it I think I can get it built. I was able to get it written out to the server folder now. Theres some more I need to do with, but thanks again I may be posting another questions possibly if i can't get the rest to work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top