Splitting file into multiple files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Paul73
    New Member
    • Sep 2007
    • 13

    Splitting file into multiple files

    Hi everyone,

    I have a file that needs to be split into multiple files. A code determines when the split will occur. The problem I'm having is the number of different codes varies each time I'll run the program. For example, one time the file may look like this:

    Code1, Name, address, city st zip
    Code2, Name, address, city st zip
    Code3, Name, address, city st zip

    So this file would need three output files. The next file may look like this:

    Code1, Name, address, city st zip
    Code2, Name, address, city st zip
    Code3, Name, address, city st zip
    Code4, Name, address, city st zip
    Code5, Name, address, city st zip

    So this file would need five output files.

    Does anyone know how to code this? If it was a set number of output files each time it wouldn't be hard for me. But having a variable number of output files is causing me trouble. Any help is greatly appreciated.

    Paul
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Maybe we need to see the code you have so far.

    But basically you read the one file
    Loop for each line read in.
    Get the code in that line
    Append that line to the file for that line.
    end loop

    Comment

    • Paul73
      New Member
      • Sep 2007
      • 13

      #3
      Hi tlhintoq,

      Thanks for your response. Sadly, I don't have any code. I'm flat-out stuck on this. How could you append to multiple files, each with different filenames? That's what's really confusing me.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Make a text file of the code name...
        code1.txt
        code2.txt
        etc.

        When you read a line you get the code: 'code1'
        That tells you the name of the file to append to.
        append to code1.txt

        Reading this might help
        Open and append to a log file using the StreamWriter and StreamReader classes in .NET, which write characters to and read characters from streams.

        Comment

        • Paul73
          New Member
          • Sep 2007
          • 13

          #5
          That is helpful. Thanks!

          Comment

          Working...