views:

352

answers:

3

Ok, I am importing and parsing csv files in a C# app. I started out doing a simple ReadLine() and splitting on commas but it turns out some of the text fields have commas in them. So I started to roll my own splitting function. Fortunately, it was only a few minutes before the "Hey stupid, someone's probably already done this?" light came on and, after a few minutes of searching, realized I could just do an OleDb connection to the directory and import them that way. That worked like a champ, until I ran into a few files with commas and parentheses in the file names. Then it blew up. Any suggestions on getting it to import from files like that? And no, I have no control over the input file names. Some will probably be read directly from CD, so I can't temporarily change the file name.

+1  A: 

Copy those files to the %TEMP% folder, giving them names create by Path.GetTempFileName(). You just need to keep track of all the file names in order to delete them afterwards.

Not very efficient, but would work ;-)

Treb
+3  A: 

I have used this CsvReader, works very well.

Sunny
Seconded (and +1)
Marc Gravell
A: 

You can split on commas but massage the data by searching for pairs of fields that begin with and end with double-quotes and then recombine them.

You can manually walk the lines looking for commas while setting a quoteFound boolean on and off as you encounter double-quotes and ignore any commas encountered when quoteFound is true.

You could copy the source to the application directory as the first thing, renaming all files by adding a ".csv" to the end.

I'm pretty sure that I did this in a pleasingly clever manner with regex but I can't figure out which of my projects it was so I don't have code to provide.

ETA: I also think you can use the SqlBulkCopy class if you're reading into SQL Server and then you can query it however you need to.

clweeks