Solved

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

Posted on 2016-11-01
6
24 Views
Last Modified: 2016-11-02
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.
0
Comment
Question by:excelismagic
  • 3
  • 3
6 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 41868868
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

0
 
LVL 3

Author Comment

by:excelismagic
ID: 41868906
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.
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 41868918
What is the value of strtext at the error ( Which line as well)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:excelismagic
ID: 41868970
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.
0
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 41869617
then 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
aFilesList = Split(strFilesList, vbCrLf)
Workbooks.Add
Range("A1").Resize(UBound(aFilesList) + 1) = WorksheetFunction.Transpose(aFilesList)
End Sub

Open in new window

0
 
LVL 3

Author Comment

by:excelismagic
ID: 41869730
thanks very much Rgonzo.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Update Modification 14 47
Pivot help - Display only Is Not Null 7 17
Excel Dynamic Chart Title Not Working 21 28
VBA Excel: Get short date v2 15 0
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now