1

Hi all i have been trying to search a specified directory and all sub directories for all files that have the specified file extension. However the inbuilt command is useless as it errors up and dies if you dont have access to a directory. So here's what i have at the moment:

 Private Function dirSearch(ByVal path As String, Optional ByVal searchpattern As String = ".exe") As String()
    Dim di As New DirectoryInfo(path)
    Dim fi As FileInfo
    Dim filelist() As String
    Dim i As Integer = 0
    For Each fi In di.GetFiles
        If System.IO.Path.GetExtension(fi.FullName).ToLower = searchpattern Then
            filelist(i) = fi.FullName
            i += 1
        End If
    Next
    Return filelist
 End Function

However i get an "System.NullReferenceException: Object reference not set to an instance of an object." when i try to access the data stored inside the filelist string array.

Any idea's on what im doing wrong?

crackruckles
  • 336
  • 2
  • 5
  • 21

2 Answers2

3

You didn't instantiate the Dim filelist() As String array. Try di.GetFiles(searchPattern)

Dim files() as FileInfo = di.GetFiles(searchPattern)

Use static method Directory.GetFiles that returns an array string

Dim files =  Directory.GetFiles(Path,searchPattern,searchOption)

Demo:

 Dim files() As String
 files = Directory.GetFiles(path, "*.exe", SearchOption.TopDirectoryOnly)
 For Each FileName As String In files
     Console.WriteLine(FileName)
 Next

Recursive directory traversal:

   Sub Main()
        Dim path = "c:\jam"
        Dim fileList As New List(Of String)

        GetAllAccessibleFiles(path, fileList)

        'Convert List<T> to string array if you want
        Dim files As String() = fileList.ToArray

        For Each s As String In fileList
            Console.WriteLine(s)
        Next
    End Sub

    Sub GetAllAccessibleFiles(path As String, filelist As List(Of String))
        For Each file As String In Directory.GetFiles(path, "*.*")
            filelist.Add(file)
        Next
        For Each dir As String In Directory.GetDirectories(path)
            Try
                GetAllAccessibleFiles(dir, filelist)
            Catch ex As Exception

            End Try
        Next
    End Sub
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • I have tried the 'Dim files() as FileInfo = di.GetFiles(searchPattern)' but i get value of type 1-dimensional array of System.IO.FileInfo cannot be converted to System.IO.FileInfo – crackruckles Dec 30 '11 at 06:15
  • ok so i got your way to work but when i change it to SearchOption.AllDirectories i get issues when it hits a folder that you dont have access to. I need it to be able to ignore folders it cant access and move on. Any Idea's this is the last thing i need to do to finish off this little app? – crackruckles Dec 30 '11 at 10:54
  • Thanks so much for your help with this, you are a life saver. – crackruckles Dec 31 '11 at 02:18
1

Use System.IO.Directory.EnumerateFiles method and pass SearchOption.AllDirectories in to traverse the tree using a specific search pattern. Here is an example:

foreach (var e in Directory.EnumerateFiles("C:\\windows", "*.dll", SearchOption.AllDirectories))
{
    Console.WriteLine(e);
}
doogle
  • 3,376
  • 18
  • 23