Loop through directories looking for files

Sarge75

New Member
Joined
Sep 20, 2021
Messages
1
Office Version
  1. 2013
Platform
  1. Windows
I have a table in a DB that contains about 75 network directories. I would like to create a function that will scan all the directories and return the file name and path for all files that have a last modified equal to 2/12/2021. I would like those files that meet the criteria to be written to another existing table in the same DB.

Can anyone offer me direction to get this accomplished?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You would loop over your table records and use the directory path as a starting point. Then you would have to loop over each file in that directory folder and check if the file property (modified date) meets your criteria. Not sure what you want to write to the other table but I presume it is the complete path to the files whose date meets the criteria. That would require running append sql (INSERT INTO...) within the inner loop to append to your other table. When no more files meet the criteria the inner loop would exit and the outer loop would then move on to the next directory path that is stored in your first mentioned table. The inner loop would then repeat until there are no more path records from the main table.

FileSystemObject is what you'll need for this. This link shows how to get the modified date and a bit that will show you how to get at the files in a directory. You will need a reference in your code project to Microsoft Scripting Runtime. Hopefully I remember that name correctly.
 
Upvote 0
create a query in Access that selects the network directories from your table
then use the following code to open the query, loop through the query one row at a time
and print the name of the network directory
pretty sure it'll work
once you get this working change the debug.print to do something with the Dir function

Code:
sub doit()
    
    dim s as string
    
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim qdf As DAO.QueryDef
     
    Set db = CurrentDb
    
    Set qdf = db.QueryDefs("what ever the name of your query is")
     
    Set rst = qdf.OpenRecordset()
    
    With rst
        Do Until .EOF
            s = rst("what ever the name of your field is")
            debug.print s
            
            .MoveNext         'Move to the next Record
        Loop
    End With
    
    rst.Close
    Set rst = Nothing
    Set qdf = Nothing
    Set db = Nothing
    
end sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,084
Messages
6,123,028
Members
449,092
Latest member
ikke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top