Solved

CREATE A FIXED WIDTH TEXT FILE from a template or other input method.

Posted on 2004-09-30
7
579 Views
Last Modified: 2008-02-01
What methods are available for creating fixed width text files.  I need to create a text file either by converting an excel template or some other method.  What I'm asking is what methods/functions are available to create a new text file and output/write fixed width data into it.  Thanks!
0
Comment
Question by:mbosch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 4

Expert Comment

by:srcalc
ID: 12196581
I'm not sure how in depth you plan to get in this, but if you are using a dataset or some similar data handling object, you can use the System.Data.OleDB namespace. Check out this web page:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/html/scripting03092004.asp
0
 
LVL 3

Expert Comment

by:jazduck
ID: 12197550
Cut/Paste this code, it will create a fixed line-length file from whatever you enter into the textbox.  I dont work with excel, so reading from your spreadsheet/template etc ill leave upto you, but for writing fixed line-length text files, this does the trick.

Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(81, 163)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(131, 29)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Write File"
        '
        'TextBox1
        '
        Me.TextBox1.AutoSize = False
        Me.TextBox1.Location = New System.Drawing.Point(13, 13)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(267, 143)
        Me.TextBox1.TabIndex = 1
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim strFixedLength As String
    Dim strTemp As String
    Dim position As Integer
    Dim i As Integer

    Sub WriteFile(ByVal Data As String)
        Dim Stream As FileStream
        Stream = New FileStream("C:\test\test.txt", FileMode.Append)
        Dim Writer As New StreamWriter(Stream)
        Writer.WriteLine(Data)
        Writer.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Writing a file with line length of 10 characters.
        'To adjust length of line you should adjust the
        'Step 10 to Step xx and the 3rd parameter of the
        'Mid expression to match.
        If File.Exists("C:\test\test.txt") Then
            File.Delete("C:\test\test.txt")
        End If

        strTemp = TextBox1.Text
        For i = 1 To Len(TextBox1.Text) Step 10
            strFixedLength = Mid(strTemp, i, 10)
            WriteFile(strFixedLength)
        Next

    End Sub

End Class

Regards
Jaz
0
 
LVL 3

Expert Comment

by:jazduck
ID: 12197824
I just realised i wrote that in VB.NET 2005 Beta1 - The windows form designer code may (or may not) work with previous versions, i have noticed some differences.  In the event that the designer code doesnt work, just delete the designer region, and draw one multiline textbox, and one button onto your form.

Jaz
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Accepted Solution

by:
Bob Learned earned 125 total points
ID: 12199617
You can use string padding and StreamWriter to accomplish this.

Bob
0
 
LVL 3

Expert Comment

by:jazduck
ID: 12245685
mbosch,

Could you please explain what was wrong with my answer, or how LearnedOne's assisted more.

Thanks,
Jaz
0
 
LVL 1

Author Comment

by:mbosch
ID: 12250776
Its just the method I used, but I suppose you're answer is still a correct answer.  How do I adjust the points in this thing?
0
 
LVL 3

Expert Comment

by:jazduck
ID: 12255869
It would be a CS request to adjust points, the usual deciding factor in who you give points to is the answer that 'helps you the most'.  If more than one answer did help you then you should split them.  I have no problem with you awarding points to LearnedOne, as his answer is correct, I was just querying how it assisted you more than a full solution.  Please only ask CS to split if my answer did help you, if it didnt, just let LearnedOne have them.

Regards
Jaz
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question