Results 1 to 6 of 6

Thread: Read from text file and write to text file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    76

    Read from text file and write to text file

    I'm not sure how to go about doing this. I know how to read from a textfile and write to a text delimited file, but I need to be able to extract specific data from the text file that I'm reading and not sure how to go about doing that. Here's an example of the file that I'm reading from and then an example of what data it is extracting and write to 3 text delimated files.

    Text file to read from:

    VB Code:
    1. CDL8GL02122 PR045050000012345   CHECKS          03
    2. CDL8GL02322 PR045050000234334   DED B           03
    3. CDL8GL09323 PR045050008821677   LTD         03
    4. CDL8GL50203-30  PR045050003239083   REGULAR EARNINGS    03
    5. CDL8GL50203-44  PR045050000022234   DED 29          03
    6. CDL8GL51232-51  PR045050000776757   DED 29          03
    7. CDL8GL52332-32  PR045050002343234   VOLUNTARY LIFE      03
    8. CDL8GL52332-31  PR045050003587821   REGULAR EARNINGS    03
    9. CDL8GL52332-33  PR045050002324879   REGULAR EARNINGS    03
    10. CDL8GL52332-34  PR045050022387767   REGULAR EARNINGS    03
    11. CDL8GL60923-23  PR045050000589876   VOLUNTARY LIFE      03
    12. CDL8GL70934-22  PR045050000334452   VOLUNTARY LIFE      03
    13. CDL8GL70983-25  PR045050000234323   DED 29          03

    After reading it from the source textfile, it will write it into 3 different files depending what is from the source textfile.

    VB Code:
    1. File#1
    2. 2122        ADP 0450    0         123.45
    3. 2322        ADP 0450    0        2343.34
    4. 9323        ADP 0450    0       88216.77
    5. 70983-25    ADP 0450    2343.23            0
    6. 70934-22    ADP 0450    3344.52            0
    7.  
    8.  
    9. FILE#2
    10. 50203-30    ADP 0450    32390.83           0
    11. 50203-44        ADP 0450          222.34           0
    12. 51232-51    ADP 0450     7767.57           0
    13. 52322-32    ADP 0450    23432.34           0
    14. 52322-31    ADP 0450           0    35878.21
    15.  
    16.  
    17. FILE#3
    18. 63223-33    ADP 0450    23248.79           0
    19. 62332-34    ADP 0450       223877.67           0
    20. 60923-23    ADP 0450           0     5898.76

    What I need help with is how would I trim each line so that it would pull specific data from the source file per line and then write it to a text delimited file depending on what is the account number? The account number from the above example are the ones beginning with "CDL" and will always stay like that.

    I was thinking about reading it to an array and then trim each strings, but I'm not really sure. I need to be able to put these into each cell within a row from the text delimited file. Can someone with experience help guide me on what's the best approach? Let me know if you need more details.

    Thanks,

    Chris

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Read from text file and write to text file

    How about ADO.NET? You can use ADO.NET to read text files. You can get the connectionstring for it form www.connectionstrings.com

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    76

    Re: Read from text file and write to text file

    Thanks for the help. If I was to use ADO.NET to read the textfile, can I use substring to pull what I need from each line it reads?

    I need to take one line and pull separate data from there and then put it into a delimited text file. So for example the data below will be in 4 cells:

    VB Code:
    1. 2122        ADP 0450    0         123.45

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Read from text file and write to text file

    Well what code do you have right now?? Are you pulling the data you need already? Is the prob just in the writing of the files?? Also, where is the money being pulled from?

  5. #5
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Read from text file and write to text file

    Is your original file delimited by tabs, or are those spaces? If it's delimited by tabs, you can use those to separate it via ADO.NET. If they are just lined up by spaces, you can use the substring function to select the fields.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    76

    Re: Read from text file and write to text file

    The problem I'm having is how would I write to another file and be able to extract specific data from each line item. As you can see one line has four parts to it. Depending on the account number, the money could be either credit or debit.

    As for where I"m getting the money here's is where it's being pulled from.

    VB Code:
    1. CDL8GL02122 PR0450500000[B]12345[/B]    CHECKS          03

    It will automatically insert a decimal right before the last 2 digits. So the money will look like 123.45.

    From my understanding this would be very easy if I'm pulling the data from a database and use a case statement for my filter with a substring to extract specific part of the data, then put it into a csv file, but with reading from another file I'm not sure.

    Let me try coding using substrings and if I get stuck I will post again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width