make a CSV from a file name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jchick

    #1

    make a CSV from a file name

    Can anyone give me a clue to build a VB.NET program to take a file name
    that looks like this:

    $_1234_99999999 _7777777777_000 00000_$_A.tif

    And make it into a CSV file to look like this:

    1234,99999999,7 777777777,00000 000,A,c:\scans\ $_1234_99999999 _7777777777_000 00000_$_A.tif


    I'd like to read through a directory with a number of these .tif files
    and create a CSV that can I can use in another process. I just don't
    really know where to start since I'm such a noob.

    Perhaps there is already code out there to do this very thing.

    Thanks for any help

  • I Don't Like Spam

    #2
    Re: make a CSV from a file name

    Jchick wrote:[color=blue]
    > Can anyone give me a clue to build a VB.NET program to take a file name
    > that looks like this:
    >
    > $_1234_99999999 _7777777777_000 00000_$_A.tif
    >
    > And make it into a CSV file to look like this:
    >
    > 1234,99999999,7 777777777,00000 000,A,c:\scans\ $_1234_99999999 _7777777777_000 00000_$_A.tif
    >
    >
    > I'd like to read through a directory with a number of these .tif files
    > and create a CSV that can I can use in another process. I just don't
    > really know where to start since I'm such a noob.
    >
    > Perhaps there is already code out there to do this very thing.
    >
    > Thanks for any help
    >[/color]

    You need to research several things.

    The System.IO.Direc tory class has a method called Get Files
    Dim Files() as String
    Files = System.IO.Direc tory.GetFiles(" C:\scans", "*.tif")
    For each FileName as String in Files
    Debug.Writeline (FileName)
    Next

    That will get you all the files in the scans directory and write them to
    the output window.

    Then you need to research creating a file for your CSV
    Look at the TextWriter file in the System.IO namespace.

    Then build your line string to write out:
    Dim Split() as string
    Split = FileName.Split( "_")
    Dim Result as new System.Text StringBuilder
    For ii as integer = 0 to Split.GetUpperB ound(0)
    Result.append(S plit(ii))
    Result.append(" ,")
    Next
    Debug.WriteLine (Result.ToStrin g())

    So do this in you file loop above. That will split the filename by
    every _ That's not perfect for what you were looking for but it's the idea.

    Hope this gets you started.
    Chris


    Comment

    • Al Reid

      #3
      Re: make a CSV from a file name

      "Jchick" <jchickering@gm ail.com> wrote in message
      news:1142641315 .003509.219230@ i40g2000cwc.goo glegroups.com.. .[color=blue]
      > Can anyone give me a clue to build a VB.NET program to take a file name
      > that looks like this:
      >
      > $_1234_99999999 _7777777777_000 00000_$_A.tif
      >
      > And make it into a CSV file to look like this:
      >
      > 1234,99999999,7 777777777,00000 000,A,c:\scans\ $_1234_99999999 _7777777777_000 00000_$_A.tif
      >
      >
      > I'd like to read through a directory with a number of these .tif files
      > and create a CSV that can I can use in another process. I just don't
      > really know where to start since I'm such a noob.
      >
      > Perhaps there is already code out there to do this very thing.
      >
      > Thanks for any help
      >[/color]

      I assume you can retrieve a list of file names from the directory. To parse
      it into the CSV format you can try something like the following:

      Dim sFileName As String =
      "c:\scans\$_123 4_99999999_7777 777777_00000000 _$_A.tif"

      Dim sCSV As String = Join(Split(IO.P ath.GetFileName (sFileName).Rep lace("$_",
      "").Replace(".t if", ""), "_"), ",") & "," & sFileName

      Debug.Print(sCS V)



      Watch for line wrapping.



      --

      Al Reid


      Comment

      • Jchick

        #4
        Re: make a CSV from a file name

        Wow, Al, thanks for that. I like the approach. I'm not sure how to
        retrieve a list of file names though. I supposed after getting that
        list of files you can use the above code in some kind of loop. I am
        not familiar with the Debug.Print command. Ideas?

        Tks

        JChick

        Comment

        • Jchick

          #5
          Re: make a CSV from a file name

          Geez, I guess I didn't look at this close enough. Chris- thanks for the
          reply. I'll take your code and Al's code and see what I can come up
          with. Thanks guys.

          Comment

          • Jchick

            #6
            Re: make a CSV from a file name

            OK, I give up. I couldn't make it work. I am much too ignorant. Anybody
            else got any ideas on how to make this work or sample code? Thanks!

            Comment

            • VHD50

              #7
              Re: make a CSV from a file name

              Hi Jchick,
              I already replied to your other post, but...
              Suppose you can get the file name of the CSV file...

              Dim oldCSVFileName As String = "$_1234_9999999 9_7777777777_00 000000_$_A.tif"
              Dim dir As String = "C:\Scans\"
              Dim parts() As String
              Dim strTemp As String = oldCSVFileName. Replace("$_", "") 'Remove $_ in front
              strTemp = strTemp.Replace ("_$", "") 'Remove _$ near the end
              strTemp = strTemp.Replace (".tif", "") 'Remove .tif at the end
              'strTemp now is just "1234_99999999_ 7777777777_0000 0000_A"
              'Split it into parts
              parts = strTemp.Split(" _")

              'Start building a csv line
              Dim i As Integer
              Dim line As String
              line = parts(0)
              For i = 1 To parts.Length - 1
              line &= "," & parts(i)
              Next

              'At this point, the value of line is 1234,99999999,7 777777777,00000 000,A
              'Just need to add the directory and file name to it then we're done
              line &= "," & dir & oldCSVFileName & vbCrLf

              'Done.
              Hope this helps,
              VHD50.

              "Al Reid" wrote:
              [color=blue]
              > "Jchick" <jchickering@gm ail.com> wrote in message
              > news:1142641315 .003509.219230@ i40g2000cwc.goo glegroups.com.. .[color=green]
              > > Can anyone give me a clue to build a VB.NET program to take a file name
              > > that looks like this:
              > >
              > > $_1234_99999999 _7777777777_000 00000_$_A.tif
              > >
              > > And make it into a CSV file to look like this:
              > >
              > > 1234,99999999,7 777777777,00000 000,A,c:\scans\ $_1234_99999999 _7777777777_000 00000_$_A.tif
              > >
              > >
              > > I'd like to read through a directory with a number of these .tif files
              > > and create a CSV that can I can use in another process. I just don't
              > > really know where to start since I'm such a noob.
              > >
              > > Perhaps there is already code out there to do this very thing.
              > >
              > > Thanks for any help
              > >[/color]
              >
              > I assume you can retrieve a list of file names from the directory. To parse
              > it into the CSV format you can try something like the following:
              >
              > Dim sFileName As String =
              > "c:\scans\$_123 4_99999999_7777 777777_00000000 _$_A.tif"
              >
              > Dim sCSV As String = Join(Split(IO.P ath.GetFileName (sFileName).Rep lace("$_",
              > "").Replace(".t if", ""), "_"), ",") & "," & sFileName
              >
              > Debug.Print(sCS V)
              >
              >
              >
              > Watch for line wrapping.
              >
              >
              >
              > --
              >
              > Al Reid
              >
              >
              >[/color]

              Comment

              Working...