+ Reply to Thread
Results 1 to 7 of 7

Search for old files by last accessed from a designated folder

Hybrid View

  1. #1
    Registered User
    Join Date
    03-08-2010
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    6

    Search for old files by last accessed from a designated folder

    I want users to be able to see a list of files they last accessed over a period of time, then to select through a multiselect listbox and kill the files. My problem comes when trying to import the last accessed date along with the file details(Which the code below does quite adequately). The displaying and killing of the files isn't a problem it's just finding the last accessed date and display in the column next to the file details. This my first post so please be nice to me. Thanks in advance Dave
    Private Sub Searchfile_Click()
        Dim fs As FileSearch, ws As Worksheet, i As Long
        Set fs = Application.FileSearch
        With fs
            .SearchSubFolders = True
            .FileType = msoFileTypeAllFiles
            .LookIn = "C:\Users\Documents" 'modify this to where you want to serach
            If .Execute > 0 Then
                Set ws = Sheets("Sheet1")
                For i = 1 To .FoundFiles.Count
                    ws.Cells(i, 1) = .FoundFiles(i)
                       Next
            Else
                 MsgBox "No files found"
            End If
        End With
    End Sub
    Last edited by Leith Ross; 03-24-2010 at 05:46 PM. Reason: Addded Code Tags

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Search for old files by last accessed from a designated folder

    Hello bodiewil,

    You will need to need to use the File System Object to get the last modified date and to recurse the subfolders. Excel 2007 and later no longer uses the Application.FileSearch. The last argument of this macro is a boolean value. True will recurse through all the subfolders of the source folder listing the files and false lists only the files in the source folder.
    'Written: March 24, 2010
    'Author:  Leith Ross
    
    Sub ListFilesInAllFolders(ByVal SourceFolderName As String, ByVal IncludeSubfolders As Boolean)
    
      Dim FSO As Object
      Dim SourceFolder As Object
      Dim SubFolder As Object
      Dim FileItem As Object
      Dim R As Long
      
         Set FSO = CreateObject("Scripting.FileSystemObject")
         Set SourceFolder = FSO.GetFolder(SourceFolderName)
          
          'Display file properties in columns "A:B" starting at row 1
           For Each FileItem In SourceFolder.Files
             R = R + 1
             Cells(R, "A") = FileItem.Name
             Cells(R, "B") = FileItem.DateLastModified
           Next FileItem
           
           If IncludeSubfolders Then
             For Each SubFolder In SourceFolder.SubFolders
               ListFilesInFolder SubFolder.Path, True
             Next SubFolder
           End If
           
        Columns("A:B").AutoFit
        
        Set FileItem = Nothing
        Set SourceFolder = Nothing
        Set FSO = Nothing
        
    End Sub

    Calling the macro
    Private Sub Searchfile_Click()
        ListFilesInAllFolders "C:\Documents and Settings\Owner\My Documents", True
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    03-08-2010
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Search for old files by last accessed from a designated folder

    Hiya Leith

    Thank you so much for this, all works fine until it tries to access the sub folders and the code debugs. Is there anyway that I get last accessed and not modified. Otherwise it gives me exactly what I require.

    If IncludeSubfolders Then
    For Each SubFolder In SourceFolder.SubFolders
    ListFilesInFolder SubFolder.Path, True
    Next SubFolder
    End If

    So impressed with the response, thank you !!!!

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Search for old files by last accessed from a designated folder

    Hello bodiewil,

    I don't know why I used DateLastModified property. Just change that to DateLastAccessed.

    The other problem was a typo. That should read....
           If IncludeSubfolders Then
             For Each SubFolder In SourceFolder.SubFolders
               ListFilesInAllFolders SubFolder.Path, True
             Next SubFolder
           End If

  5. #5
    Registered User
    Join Date
    03-08-2010
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Search for old files by last accessed from a designated folder

    Pure Genius Leith

    Now to be a pain in the neck what would I have to add to show Filesize.

    Thanks so much for everything

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Search for old files by last accessed from a designated folder

    Hello bodiewil,

    To add the file size ...
          'Display file properties in columns "A:C" starting at row 1
           For Each FileItem In SourceFolder.Files
             R = R + 1
             Cells(R, "A") = FileItem.Name
             Cells(R, "B") = FileItem.DateLastModified
             Cells(R, "C") = FileItem.Size
           Next FileItem

  7. #7
    Registered User
    Join Date
    03-08-2010
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Search for old files by last accessed from a designated folder

    Thanks Leith just perfect....... Hope to speak to you soon

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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