Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB.net code to Read all lines in Fixed Width text file and add characters in specific locations

Posted on 2009-07-02
4
Medium Priority
?
624 Views
Last Modified: 2012-05-07
I am trying to code a small application which addresses a formatting issue in a text file received every month, its a fixed width file which has dates in a specific location such as 20090701 i need to change every line to read 2009-07-01 I was thinking maybe using streamreader or the like but cant quite figure this one out
0
Comment
Question by:nemesis1210
[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
  • 2
4 Comments
 
LVL 8

Expert Comment

by:bhmahler
ID: 24765931
You can read all the lines of the file into a string array using System.IO.File.ReadAllLines("PATH TO FILE"), then you can jsut loop through the array and adjust the lines as needed and rewrite them to the file.
0
 
LVL 2

Author Comment

by:nemesis1210
ID: 24766023
any chance you can point me to some sample code
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 24767045
Here's some example code:
        Dim FileName As String = "c:\some\file.txt"
        Dim Lines() As String = System.IO.File.ReadAllLines(FileName)
 
        Dim sb As New System.Text.StringBuilder
        For i As Integer = 0 To Lines.GetUpperBound(0)
            ' Pretend your date starts at Index 60.   (don't forget that indexes are ZERO based!)
            ' 20090701 --> 2009-07-01
            ' So we work BACKWARDS and insert at 66 and 64.
 
            ' Clear the StringBuilder and add the Current Line:
            sb.Length = 0
            sb.Append(Lines(i))
 
            ' Insert the Dashes
            sb.Insert(66, "-")
            sb.Insert(64, "-")
 
            ' put the changed line back in the array
            Lines(i) = sb.ToString
        Next
 
        System.IO.File.WriteAllLines(FileName, Lines)

Open in new window

0
 
LVL 2

Author Closing Comment

by:nemesis1210
ID: 31599290
Worked Perfectly thank you so much
0

Featured Post

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!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

618 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