Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scan for files in directory tree

Status
Not open for further replies.

jbradley

Programmer
Sep 7, 2001
248
US
I need to automate deleting files from a directory tree that are over 150 days old. I found the code below which I'm using as a proof of concept. It processes the files in the root but I can't figure out how to go below the root directory.
Code:
    Dim objFSO As New FileSystemObject
    Dim objFile As File
    Dim objFolder As Folder
    
    Set objFolder = objFSO.GetFolder("Root Directory Path")
    
    For Each objFile In objFolder.Files
        List1.AddItem objFile.Name & ", " & objFile.DateCreated
        
        If DateDiff("d", objFile.DateCreated, Date) > 150 Then
            List2.AddItem objFile.Name & ", " & objFile.DateCreated
        End If
    DoEvents
    Next objFile
    
    Set objFSO = Nothing
    Set objFile = Nothing
    Set objFolder = Nothing
What do I need to add to recurse the rest of the folders?
 
A keyword search in this forum should reveal multiple recursive solutions for walking a directory tree. If yiou can't find anything apprpriate or don't completely understand what you do find, just let us know and we'll see what more we can do to help
 
I would have thought so ...


Sorry, the query "recursive directrory" produced no results.

From Advanced Search



MichaelRed


 
I actually found how to do what I needed ages ago.

[blue]
Code:
Private Sub GetFiles(strFolder As String, Optional Subfolders As Boolean = True)

  On Error Resume Next
                
  Dim m_Folder  As Folder
  Dim m_File    As File
[green]  
  'We're looking for files. If we find folders we drill down. [/green]
  For Each m_File In fso.GetFolder(strFolder).Files [green]
    'If we find a file we check the create date.
    'If it's more than 150 days old we process it. [/green]
    If DateDiff("d", m_File.DateCreated, Date) > 150 Then [green]
        'We only want to delete Zip or audio files [/green]
        Select Case UCase(Right$(Dir(m_File.Path), 3))
            Case "ZIP", "GSM", "WAV", "VOX", "MP3"
                m_File.Delete True
        End Select
    End If
  Next
[green]  
  'Here is where we recurse the folders we find. [/green]
  If Subfolders Then
    For Each m_Folder In fso.GetFolder(strFolder).Subfolders
      GetFiles m_Folder.Path, True
    Next
  End If
  
End Sub
[/blue]
 
Thanks, I found a version using the File System Object which seemed to be working earlier this afternoon. Left it attempting to complete a rigorous task.




MichaelRed


 
thread222-609352 shows how to search directories recursively using Dir function.

Personally, I prefer API method using FindFirstFile/FindNextFile/FindClose functions.
 
I used the API before but changed because it took minutes to find each file (I have more than 50k files to search through). Now I'm using shellexecute to call "dir /a/s>file.text" then read the file in and find what I need with InStr. All in all it is many hundred times faster, more difference the more files I am looking for at the same time.
 
I checked both methods to produce a list of files on my C drive.

[tt]dir/s/a/b>file.txt[/tt] took 38 seconds, where as VB code using API functions producing similar file list took 18 seconds. I repeated it many times and results were same.

I have about 62000 files and folders on my C drive.
 
>Sorry, the query "recursive directrory" produced no results.

Sure, it will not! [wink]
 
The lines I placed in the post were taken from the screen. Unless I misspelled something, it did.



MichaelRed


 
As with Hypetia I've also always found the API method faster.

Indeed, I'm pretty sure at soem point in the last few years that we did an informal test comparing VB's Dir command, the file system object, the FindWhatever API family, (and possibly shelling the Dir command) which clearly showed that FindWhatever was the fastest solution. Can't find the thread now, though.

For what it is worth, I suspect that, since W2K, the 'DOS' Dir command actually calls the FindWhatever API family
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top