Solved

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

Posted on 2004-09-30
7
575 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
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now