Link to home
Start Free TrialLog in
Avatar of excelismagic
excelismagic

asked on

Search the selected directory and list all folders and files created from 3 days ago until today.

I need help,

i need VBA to go through all folders and sub-folders of selected directory and list all folders and files which their creation date falls between now and 3 days ago.

thanks vert much.
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try

Sub Macro()
strYourFolder = "C:\YourFolder"
Set oFS = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("cmd /c Dir """ & strYourFolder & "\*"" /s /b")
Do While Not objExecObject.StdOut.AtEndOfStream
    strtext = objExecObject.StdOut.ReadLine()
    If oFS.FolderExists(strtext) Then
        If oFS.GetFolder(strtext).DateCreated > Date - 3 Then
            strFilesList = strtext & vbCrLf & strFilesList
        End If
    Else
        If oFS.GetFile(strtext).DateCreated > Date - 3 Then
            strFilesList = strtext & vbCrLf & strFilesList
        End If
    End If
Loop
End Sub

Open in new window

Avatar of excelismagic

ASKER

thanks Rgonzo,

but it return error 53 File not Found.

although, i have folders that created just now and its name should be listed in excel. but i get the error.
What is the value of strtext at the error ( Which line as well)
it was giving error in line If oFS.GetFile(strtext).DateCreated > Date - 3 Then  which i solved it but removing the long named files and the error disappeared.  

thanks.

how do i put this now into excel sheet,

i used Debug.Print strFilesList  and it listed the details into immediate window.
but i dont know how to put the details instead of immediate window to activeworksheet.
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks very much Rgonzo.