Solved

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

Posted on 2016-11-01
6
19 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 48

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 48

Expert Comment

by:Rgonzo1971
ID: 41868918
What is the value of strtext at the error ( Which line as well)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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 48

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

708 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

20 Experts available now in Live!

Get 1:1 Help Now