+ Reply to Thread
Results 1 to 7 of 7

Scan Through All Files and Subdirectories

Hybrid View

  1. #1
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208

    Scan Through All Files and Subdirectories

    Hi everyone.

    I am trying to scan through all files within a directory and all subdirectories to find files where the name begins with certain characters. Below is code that will accomplish this for a certain directory, but I do not know how to look through the subdirectories within the specified directory. So, the issues are:

    1. How do I loop through each subdirectory within a specified directory?

    2. I am trying to return the file name, path name (separate from the file name), and modified date. I can return the name. I can return the path, but it includes the name. Is there a way to return just the path?

    3. What is the code to return the modified date of the file?

    Thanks in advance for any help.

    Jason

    Sub Scan_Files()
    
        Dim Fso, Fs, Fl
        Dim FolderSpec As String
        Dim i As Integer
        
    'set directory:
        FolderSpec = "\\Server\Files\"
        i = 0
        
        Set Fso = CreateObject("Scripting.FileSystemObject")
        Set Fs = Fso.GetFolder(FolderSpec)
        
        For Each Fl In Fs.Files
            If UCase(Left(Fl.Name, 4)) = "A323" Then
                Cells(Range("StartHere").Row + i, Range("StartHere").Column) = Fl.Name
                Cells(Range("StartHere").Row + i, Range("StartHere").Column + 1) = Fl.Path
                i = i + 1
            End If
        Next Fl
    
    End Sub
    Last edited by jasoncw; 01-06-2009 at 11:57 AM.

  2. #2
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Austin
    Posts
    660

    Hope this helps.

    I had a very similiar problem. It was solved by Rylo.
    Here's a link to the thread.
    HTML Code: 
    Within it you will find a files
    with the code he setup for me. I think you could probably
    use it for your situation. It finds specific files and provide directory paths
    w/hyperlinks.

    good luck,

    BDB

  3. #3
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Great link. Thanks a lot, BDB. That answers most of my questions. I still have one remaining question, though. Hopefully someone out there can help me.

    Is it possible to return the modified date of each of the files?

    TIA

    Jason

  4. #4
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Does anyone know if this is possible? Thanks.

  5. #5
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    There are FSO methods to get it. If you use early binding, it makes finding those easy. If you don't know fso, get the help file script56.chm.

    For whatever method, you can use VBA's FileDateTime(filename).

    For some other fso and dos methods see my post at VBAExpress.

    Here is an example FileSearch method. It won't work in 2007.
    Option Explicit
    Option Base 0
    
    
    Sub BatchRun()
      Dim f() As Variant, e As Variant
      f() = FindFiles(ThisWorkbook.Path, "BatchTest*.xls", False)
      For Each e In f()
        EditWB e
      Next e
    End Sub
    
    Sub EditWB(wbName)
      Dim wb As Workbook
      Set wb = Workbooks.Open(wbName)
      wb.Worksheets(1).Range("A1").Value = wb.Name
      wb.Close True
    End Sub
    
    Sub BigFiles()
      Dim f() As Variant, e As Variant, bigf() As Variant, i As Long
      
      f() = FindFiles(ThisWorkbook.Path, "*.xls")
      i = -1
      For Each e In f()
            'Get files larger than 20 kb
            If FileLen(e) / 1000 > 25 Then
              i = i + 1
              ReDim Preserve bigf(i)
              bigf(i) = e
            End If
      Next e
      
      If bigf(0) = "" Then Exit Sub
      Range("A:A").ClearContents
      Range("A1").Resize(UBound(bigf) + 1) = bigf()
    End Sub
    
    Function FindFiles(sRootFolder As String, sFiles As String, _
      Optional searchSubFolders As Boolean = True) As Variant
         
        Dim fs As Object
        Dim strFilename As String
        Dim i As Long, LastRow As Long
        Dim a() As Variant
         
        Set fs = Application.FileSearch
        With fs
            .LookIn = sRootFolder
            .Filename = sFiles 'set your filename or extension with wilcards if needed.
            .searchSubFolders = searchSubFolders
            LastRow = .FoundFiles.Count
            If .Execute() > 0 Then
                For i = 1 To LastRow
                    strFilename = .FoundFiles(i)
                    ReDim Preserve a(i - 1)
                    a(i - 1) = strFilename
                Next i
            Else
                MsgBox "No " & sFiles & " in " & sRootFolder & " found!", vbCritical
                End
            End If
        End With
        
        FindFiles = a()
         
    End Function

  6. #6
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Thanks so much for the info, Kenneth. I am very unfamiliar with FSO, so thank you for the help file. I need to do some reading now.

    The property for which I was looking was DateLastModified.

    Jason

  7. #7
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    If the fso help file does not display everything, close and right click it and Unblock it. From the help file:
    Function ShowFileAccessInfo(filespec)
       Dim fso, f, s
       Set fso = CreateObject("Scripting.FileSystemObject")
       Set f = fso.GetFile(filespec)
       s = UCase(filespec) & "<BR>"
       s = s & "Created: " & f.DateCreated & "<BR>"
       s = s & "Last Accessed: " & f.DateLastAccessed & "<BR>"
       s = s & "Last Modified: " & f.DateLastModified
       ShowFileAccessInfo = s
    End Function
    The API method from the API Guide:
    Private Const OPEN_EXISTING = 3
    Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
    End Type
    Private Type BY_HANDLE_FILE_INFORMATION
        dwFileAttributes As Long
        ftCreationTime As FILETIME
        ftLastAccessTime As FILETIME
        ftLastWriteTime As FILETIME
        dwVolumeSerialNumber As Long
        nFileSizeHigh As Long
        nFileSizeLow As Long
        nNumberOfLinks As Long
        nFileIndexHigh As Long
        nFileIndexLow As Long
    End Type
    
    Private Declare Function GetFileInformationByHandle Lib "kernel32" (ByVal hFile As Long, lpFileInformation As BY_HANDLE_FILE_INFORMATION) As Long
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    
    Private Sub Form_Load()
        'KPD-Team 2001
        'URL: http://www.allapi.net/
        'E-Mail: KPDTeam@allapi.net
        Dim hFile As Long, FileInfo As BY_HANDLE_FILE_INFORMATION
        'create a handle to the file 'c:\autoexec.bat'
        hFile = CreateFile("c:\autoexec.bat", 0, 0, ByVal 0&, OPEN_EXISTING, 0, ByVal 0&)
        'retrieve the file information
        GetFileInformationByHandle hFile, FileInfo
        'close the handle
        CloseHandle hFile
        'show the result
        MsgBox "File size: " + CStr(FileInfo.nFileSizeLow), vbInformation
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1