Solved

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

Posted on 2016-11-01
6
31 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 50

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 50

Expert Comment

by:Rgonzo1971
ID: 41868918
What is the value of strtext at the error ( Which line as well)
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 50

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
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 will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

829 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