make a CSV from a file name

  • Thread starter Thread starter Jchick
  • Start date Start date
J

Jchick

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

$_1234_99999999_7777777777_00000000_$_A.tif

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

1234,99999999,7777777777,00000000,A,c:\scans\$_1234_99999999_7777777777_00000000_$_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
 
Jchick said:
Can anyone give me a clue to build a VB.NET program to take a file name
that looks like this:

$_1234_99999999_7777777777_00000000_$_A.tif

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

1234,99999999,7777777777,00000000,A,c:\scans\$_1234_99999999_7777777777_00000000_$_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

You need to research several things.

The System.IO.Directory class has a method called Get Files
Dim Files() as String
Files = System.IO.Directory.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.GetUpperBound(0)
Result.append(Split(ii))
Result.append(",")
Next
Debug.WriteLine(Result.ToString())

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
 
Jchick said:
Can anyone give me a clue to build a VB.NET program to take a file name
that looks like this:

$_1234_99999999_7777777777_00000000_$_A.tif

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

1234,99999999,7777777777,00000000,A,c:\scans\$_1234_99999999_7777777777_00000000_$_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 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\$_1234_99999999_7777777777_00000000_$_A.tif"

Dim sCSV As String = Join(Split(IO.Path.GetFileName(sFileName).Replace("$_",
"").Replace(".tif", ""), "_"), ",") & "," & sFileName

Debug.Print(sCSV)



Watch for line wrapping.
 
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
 
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.
 
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!
 
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_99999999_7777777777_00000000_$_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_00000000_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,7777777777,00000000,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.
 
Back
Top