locked
How to find .csv files in subdirectories?? RRS feed

  • Question

  • Hello
    I have come up against another brick wall! 

    I have a large number of .csv files which are located within subdirectories of lets say the "c:\temp" directory.  What I need to do is find all of these csv files, open them individually, perform an operation on each and then close each one.

    What I am struggling with is the part where I find all of these csv files.  What I would ideally like to do is search through each of the subdirectories, return a filename (including complete path) as a string which I can then use in my code to open the files individually and perform the operations.

    I have been experimenting so far but have come up with nothing that works.

    Can anyone please help??!!
    Friday, July 24, 2009 11:15 AM

Answers

  • Try this. I've used the For Each loop to check for files in the My Documents folder (instead of "C:\temp") and added them to a ListBox for examples sake:

    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
         Handles Button1.Click
            For Each file As String In IO.Directory.GetFiles( _
             Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "*.csv", _
              IO.SearchOption.AllDirectories)
                ListBox1.Items.Add(file) ' Example
                ' Your code here
            Next
        End Sub
    End Class

    • Proposed as answer by Daniel Rieck Friday, July 24, 2009 12:24 PM
    • Marked as answer by Mister S Friday, July 24, 2009 12:43 PM
    Friday, July 24, 2009 11:54 AM

All replies

  • Use Directory.GetFiles with your desired pattern and option.
    Friday, July 24, 2009 11:21 AM
  • Try this. I've used the For Each loop to check for files in the My Documents folder (instead of "C:\temp") and added them to a ListBox for examples sake:

    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
         Handles Button1.Click
            For Each file As String In IO.Directory.GetFiles( _
             Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "*.csv", _
              IO.SearchOption.AllDirectories)
                ListBox1.Items.Add(file) ' Example
                ' Your code here
            Next
        End Sub
    End Class

    • Proposed as answer by Daniel Rieck Friday, July 24, 2009 12:24 PM
    • Marked as answer by Mister S Friday, July 24, 2009 12:43 PM
    Friday, July 24, 2009 11:54 AM
  • Thats great, works a treat, many thanks
    Friday, July 24, 2009 12:43 PM