-
Search within Files
Hello,
Can anyone tell me how I can search a folder of csv files to find out if one of the csv files has a particular name in ?
I'm currently searching a folder to bring back all csv files using the code below and this is working fine.
Code:
Dim filetype As String = "*.csv"
Dim dir As String = "X:\Processed"
lstv1.Items.Clear()
For Each filename As String In System.IO.Directory.GetFiles(dir, filetype)
lstv1.Items.Add(filename)
Next
But now I want to take it a step further and actually search within the files themselves.
Thanks,
-
Re: Search within Files
The only way you can do that is to open the file, read in all of its content and then do the search within that string. Or rather that would be the answer for a text file of any general type. But since this is CSV files you can use ADO.Net and query each file just like you would query any database table.
-
Re: Search within Files
Thanks for your help Joacim.
I've decided to read the files and put the contents into a textbox for now.
Code:
Dim filename As String = lstv1.FocusedItem.SubItems(0).Text
Dim ioFile As New StreamReader(filename)
Dim ioLine As String
Dim ioLines As String
ioLine = ioFile.ReadLine
ioLines = ioLine
While Not ioLine = ""
ioLine = ioFile.ReadLine
ioLines = ioLines & vbCrLf & ioLine
End While
TextBox1.Text = ioLines
ioFile.Close()
This works a treat but I'd like to get the 2nd item from the CSV (i.e. the item after the 1st comma and before the 2nd comma)
Can I do this ?
I'm obviously bringing the data in so I assume I can, but I'm not sure of the syntax I need to get what I require.