?
Solved

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

Posted on 2004-09-30
7
Medium Priority
?
580 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
Industry Leaders: 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

765 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