Hi guys, I have already posted a thread regarding this but no one has ever shed a light upon it. I want a program that searches for files (.docx, exe, ppt, txt) from all folders. Is this possible i the first place? If yes, then how?

Is this possible i the first place?

Yes it is.

If yes, then how?

Here's a subroutine from my crapbox. The extra features you'll get are: restrict files with file creation date and/or with file size. I was too lazy to take those off, but you can do it by yourself :)

' File information
Public Const TOOL_DIR_NAMEONLY As Integer = 0
Public Const TOOL_DIR_SIZE As Integer = 1
Public Const TOOL_DIR_MODIFIED As Integer = 2
Public Const TOOL_DIR_FULLPATH As Integer = 4
Public Const TOOL_DIR_DIRECTORIES_ONLY As Integer = 8
Public Const TOOL_DIR_ALL As Integer = 15

''' <summary>
''' Searches for files that match ány of the file masks in the FileMask array
''' </summary>
''' <param name="RootPath">Path to start search from</param>
''' <param name="FileMask">An array of filemasks</param>
''' <param name="FileNames">Returns matching files</param>
''' <param name="RecurseDirs">Recurse subdirectories too</param>
''' <param name="ExcludeRootPath">Strip start path away from the file names</param>
''' <param name="MinDate">Search files newer than MinDate</param>
''' <param name="MaxDate">Search files older than MaxDate</param>
''' <param name="MinSize">Search files larger than MinSize</param>
''' <param name="MaxSize">Search files smaller than MaxSize</param>
''' <param name="FileInfo">Optional parameter which tells if some extra info should be returned with the filenames. Use constant values above.</param>
''' <remarks></remarks>
Public Sub ToolDir(ByVal RootPath As String, ByVal FileMask() As String, _
  ByRef FileNames() As String, _
  ByVal RecurseDirs As Boolean, _
  ByVal ExcludeRootPath As Boolean, _
  ByVal MinDate As Date, ByVal MaxDate As Date, _
  ByVal MinSize As Integer, ByVal MaxSize As Integer, _
  Optional ByVal FileInfo As Integer = TOOL_DIR_NAMEONLY)
    '
    ' Return files
    '
    Dim DirInfo As DirectoryInfo
    Dim Files() As FileInfo
    Dim OneFile As FileInfo
    Dim TempDrive As String
    Dim TempPath As String
    Dim TempFile As String
    Dim TempExt As String
    Dim CheckMinDate As Boolean
    Dim CheckMaxDate As Boolean
    Dim CheckMinSize As Boolean
    Dim CheckMaxSize As Boolean
    Dim AcceptFile As Boolean
    Dim RootPathLength As Integer
    Dim i As Integer
    Dim j As Integer

    Try
        If MinDate <> CDate("1.1.1970") Then
            CheckMinDate = True
        Else
            CheckMinDate = False
        End If
        If MaxDate <> CDate("1.1.1970") Then
            CheckMaxDate = True
        Else
            CheckMaxDate = False
        End If
        If MinSize > 0 Then
            CheckMinSize = True
        Else
            CheckMinSize = False
        End If
        If MaxSize > 0 Then
            CheckMaxSize = True
        Else
            CheckMaxSize = False
        End If
        If FileMask.GetUpperBound(0) < 0 Then
            ReDim FileMask(0)
            FileMask(0) = "*.*"
        End If
        i = 0
        TempDrive = ""
        TempPath = ""
        TempFile = ""
        TempExt = ""
        RootPathLength = RootPath.Length + 1
        For j = 0 To FileMask.GetUpperBound(0)
            ReDim Files(0)
            If RecurseDirs Then
                DirInfo = New DirectoryInfo(RootPath)
                Files = DirInfo.GetFiles(FileMask(j), IO.SearchOption.AllDirectories)
            Else
                DirInfo = New DirectoryInfo(RootPath)
                Files = DirInfo.GetFiles(FileMask(j), IO.SearchOption.TopDirectoryOnly)
            End If

            For Each OneFile In Files
                AcceptFile = True
                If CheckMinDate Then
                    If OneFile.LastAccessTime.Date.Subtract(MinDate).Days < 0 Then
                        AcceptFile = False
                    End If
                End If
                If CheckMaxDate Then
                    If OneFile.LastAccessTime.Date.Subtract(MaxDate).Days > 0 Then
                        AcceptFile = False
                    End If
                End If
                If CheckMinSize Then
                    If OneFile.Length < MinSize * 1024 Then
                        AcceptFile = False
                    End If
                End If
                If CheckMaxSize Then
                    If OneFile.Length > MaxSize * 1024 Then
                        AcceptFile = False
                    End If
                End If
                If AcceptFile Then
                    ReDim Preserve FileNames(i)
                    If (FileInfo And TOOL_DIR_FULLPATH) = TOOL_DIR_FULLPATH Then
                        If ExcludeRootPath Then
                            FileNames(i) = OneFile.FullName.Substring( _
                              RootPathLength, OneFile.FullName.Length - RootPathLength)
                        Else
                            FileNames(i) = OneFile.FullName
                        End If
                    Else
                        FileNames(i) = OneFile.Name
                    End If
                    If (FileInfo And TOOL_DIR_SIZE) = TOOL_DIR_SIZE Then
                        FileNames(i) = FileNames(i) & Convert.ToChar(9) & OneFile.Length
                    End If
                    If (FileInfo And TOOL_DIR_MODIFIED) = TOOL_DIR_MODIFIED Then
                        FileNames(i) = FileNames(i) & Convert.ToChar(9) & OneFile.LastAccessTime
                    End If
                    i += 1
                End If
            Next
        Next j
    Catch ex As Exception
        ' Some error
    End Try

End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    Dim FileNames(0) As String

		' Search for all png-files under C:\Temp folder
    ToolDir("C:\Temp\", New String() {"*.png"}, FileNames, True, False, CDate("1.1.1970"), CDate("1.1.1970"), 0, 0)

    For Each fn As String In FileNames
        MessageBox.Show(fn)
    Next

End Sub

At the end is an example how to call this sub.

HTH

Another non-recursive version with less options.

Uses linq to get the files with the passed in extensions. Button_Click sub shows how to call it. Get files takes a paramarray fileExtensions paramater. Pass in all the extensions to check.

DirectoryInfo.GetFiles has a search all directories option which will do the same thing. However, if access is denied it will throw an exception and you will get nothing. This catches the exception and keeps going. So you will get all files that you have access to.

I usually validate things before processing them but I found that it was faster to just catch the access violation error instead of checking for it.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim FileNames As List(Of String) = GetFiles("c:\", ".docx", ".exe", ".txt")

    End Sub

    Private Function GetFiles(ByVal rootDirectory As String, ByVal ParamArray fileExtensions As String()) As List(Of String)
        'validate root directory exists

        Dim DirectoryStack As New Stack(Of String)
        Dim DirectoryInfo As IO.DirectoryInfo
        Dim FoundFiles As New List(Of String)
        Dim CurrentDirectory As String

        DirectoryStack.Push(rootDirectory)
        Do While DirectoryStack.Count > 0
            CurrentDirectory = DirectoryStack.Pop
            Try
                For Each SubDirectory As String In IO.Directory.GetDirectories(CurrentDirectory)
                    DirectoryStack.Push(SubDirectory)
                Next

                DirectoryInfo = New IO.DirectoryInfo(CurrentDirectory)

                'Calling GetFiles which will return all files in the
                'directory.  The Linq Where extension will only return
                'the files with the extensions passed in.
                'The Linq Select extension will only select the name
                'of the file instead of the entirer fileinfo object.
                FoundFiles.AddRange(DirectoryInfo.GetFiles.Where(Function(fileInfo) fileExtensions.Contains(fileInfo.Extension)).Select(Function(fileName) fileName.FullName).ToArray)

            Catch 'Access violation
            End Try
        Loop

        Return FoundFiles

    End Function

Guys I'm overwhelmed of this code you have given to me. Is there no other simpler codes out there? But anyway, thank you.

I tried using the above code, it did not work for the first time. It did nothing, I worked around it, and I think I now have a program to search files. Thanks!!!!!!!!!!!!

O but then the latter code,the one posted by unhnd_Exception, yes it did gets all the files from a directory but it there are files and folder that it repeatedly gets so that the response time is beeing affected. There are files that have duplicates because of this... How do I solve this?

Hi guys, I have already posted a thread regarding this but no one has ever shed a light upon it. I want a program that searches for files (.docx, exe, ppt, txt) from all folders. Is this possible i the first place? If yes, then how?

when u say u want to search for files, what do u mean. are u saying searching from all computer drives or a specific drive?
yes its possible but give more detail of how u would want to search.
if u ar using windows u can use the default windows search but if u want some code give more detail of how u want to do it.

This question has already been answered. Start a new discussion instead.