Consulting

Results 1 to 12 of 12

Thread: List of files in directory by date created

  1. #1
    VBAX Regular
    Joined
    Oct 2011
    Posts
    6
    Location

    Question List of files in directory by date created

    Hello,
    I would be grateful for any help. This is what I am trying to do. I am using following macro that displays all the files, subfolders and files under subfolders of folder with both date created and date modified then output the result to .txt or .xls file.

    The macro which I have used works fine but what I am trying to do is to run macro for files only created in last two months or between certain dates.

    This macro does not allow me to do that!!!

    I would appreciate any help.

    Thanks in advance.
    Bhaven

    [vba]Public X()
    Public i As Long
    Public objShell, objFolder, objFolderItem
    Public FSO, oFolder, Fil

    Sub MainExtractData()
    Dim NewSht As Worksheet
    Dim MainFolderName As String
    Dim TimeLimit As Long, StartTime As Double
    ReDim X(1 To 65536, 1 To 11)
    Set objShell = CreateObject("Shell.Application")
    TimeLimit = Application.InputBox("Please enter the maximum time that you wish this code to run for in minutes" & vbNewLine & vbNewLine & _
    "Leave this at zero for unlimited runtime", "Time Check box", 0)
    StartTime = Timer
    ' This is the part I need to change to create a Table with these field names.
    Application.ScreenUpdating = False
    MainFolderName = BrowseForFolder()
    Set NewSht = ThisWorkbook.Sheets.Add
    X(1, 1) = "Path"
    X(1, 2) = "File Name"
    X(1, 3) = "Last Accessed"
    X(1, 4) = "Last Modified"
    X(1, 5) = "Created"
    X(1, 6) = "Type"
    X(1, 7) = "Size"
    X(1, 8) = "Owner"
    X(1, 9) = "Author"
    X(1, 10) = "Title"
    X(1, 11) = "Comments"
    ' i is defined Publicly as a Long Integer
    i = 1
    Set FSO = CreateObject("scripting.FileSystemObject")
    Set oFolder = FSO.GetFolder(MainFolderName)
    'error handling to stop the obscure error that occurs at time when retrieving DateLastAccessed
    On Error Resume Next
    For Each Fil In oFolder.Files
    Set objFolder = objShell.Namespace(oFolder.Path)
    Set objFolderItem = objFolder.ParseName(Fil.Name)
    i = i + 1
    If i Mod 20 = 0 And TimeLimit <> 0 And Timer > (TimeLimit * 60 + StartTime) Then
    GoTo FastExit
    End If
    If i Mod 50 = 0 Then
    Application.StatusBar = "Processing File " & i
    DoEvents
    End If
    X(i, 1) = oFolder.Path
    X(i, 2) = Fil.Name
    X(i, 3) = Fil.DateLastAccessed
    X(i, 4) = Fil.DateLastModified
    X(i, 5) = Fil.DateCreated
    X(i, 6) = Fil.Type
    X(i, 7) = Fil.Size
    X(i, 8) = objFolder.GetDetailsOf(objFolderItem, 8)
    X(i, 9) = objFolder.GetDetailsOf(objFolderItem, 9)
    X(i, 10) = objFolder.GetDetailsOf(objFolderItem, 10)
    X(i, 11) = objFolder.GetDetailsOf(objFolderItem, 14)
    Next
    'Get subdirectories
    If TimeLimit = 0 Then
    Call RecursiveFolder(oFolder, 0)
    Else
    If Timer < (TimeLimit * 60 + StartTime) Then Call RecursiveFolder(oFolder, TimeLimit * 60 + StartTime)
    End If
    FastExit:
    Range("A:K") = X
    If i < 65535 Then Range(Cells(i + 1, "A"), Cells(65536, "A")).EntireRow.Delete
    Range("A:K").WrapText = False
    Range("A:K").EntireColumn.AutoFit
    Range("1:1").Font.Bold = True
    Rows("2:2").Select
    ActiveWindow.FreezePanes = True
    Range("a1").Activate
    Set FSO = Nothing
    Set objShell = Nothing
    Set oFolder = Nothing
    Set objFolder = Nothing
    Set objFolderItem = Nothing
    Set Fil = Nothing
    Application.StatusBar = ""
    Application.ScreenUpdating = True
    End Sub

    Sub RecursiveFolder(xFolder, TimeTest As Long)
    Dim SubFld
    For Each SubFld In xFolder.SubFolders
    Set oFolder = FSO.GetFolder(SubFld)
    Set objFolder = objShell.Namespace(SubFld.Path)
    For Each Fil In SubFld.Files
    Set objFolder = objShell.Namespace(oFolder.Path)
    'Problem with objFolder at times
    If Not objFolder Is Nothing Then
    Set objFolderItem = objFolder.ParseName(Fil.Name)
    i = i + 1
    If i Mod 20 = 0 And TimeTest <> 0 And Timer > TimeTest Then
    Exit Sub
    End If
    If i Mod 50 = 0 Then
    Application.StatusBar = "Processing File " & i
    DoEvents
    End If
    X(i, 1) = SubFld.Path
    X(i, 2) = Fil.Name
    X(i, 3) = Fil.DateLastAccessed
    X(i, 4) = Fil.DateLastModified
    X(i, 5) = Fil.DateCreated
    X(i, 6) = Fil.Type
    X(i, 7) = Fil.Size
    X(i, 8) = objFolder.GetDetailsOf(objFolderItem, 8)
    X(i, 9) = objFolder.GetDetailsOf(objFolderItem, 9)
    X(i, 10) = objFolder.GetDetailsOf(objFolderItem, 10)
    X(i, 11) = objFolder.GetDetailsOf(objFolderItem, 14)
    Else
    Debug.Print Fil.Path & " " & Fil.Name
    End If
    Next
    Call RecursiveFolder(SubFld, TimeTest)
    Next
    End Sub

    Function BrowseForFolder(Optional OpenAt As Variant) As Variant
    'Function purpose: To Browser for a user selected folder.
    'If the "OpenAt" path is provided, open the browser at that directory
    'NOTE: If invalid, it will open at the Desktop level
    Dim ShellApp As Object
    'Create a file browser window at the default folder
    Set ShellApp = CreateObject("Shell.Application"). _
    BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
    'Set the folder to that selected. (On error in case cancelled)
    On Error Resume Next
    BrowseForFolder = ShellApp.self.Path
    On Error GoTo 0
    'Destroy the Shell Application
    Set ShellApp = Nothing
    'Check for invalid or non-entries and send to the Invalid error
    'handler if found
    'Valid selections can begin L: (where L is a letter) or
    '\\ (as in \\servername\sharename. All others are invalid
    Select Case Mid(BrowseForFolder, 2, 1)
    Case Is = ":"
    If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
    Case Is = "\"
    If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
    Case Else
    GoTo Invalid
    End Select
    Exit Function
    Invalid:
    'If it was determined that the selection was invalid, set to False
    BrowseForFolder = False
    End Function[/vba]
    Last edited by Aussiebear; 10-27-2011 at 03:30 PM. Reason: Added vba tags to code

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    You need an IF to determine whether to add the file to the array and sheet. Obviously, you need to look at either comparing to some date minimum or date interval using DateSerial() or #thedate# using one of the Fil object date properties of:
    [vba] X(i, 3) = Fil.DateLastAccessed
    X(i, 4) = Fil.DateLastModified
    X(i, 5) = Fil.DateCreated [/vba]
    Say what minimum or range of dates and which of the three types of file dates that you want to limit. OF course you could just limit the view by a Filter.

  3. #3
    VBAX Regular
    Joined
    Oct 2011
    Posts
    6
    Location

    List of files in directory by date created

    Thank you for your response. My knowledge in macro is very basic and would not be able to make the changes you have suggested.

    I would be grateful if you could give me more guidance on how to make these changes. For dates this is what I am looking for. I want to search for files created between 01.08.2011 to 31.10.2011 and so on.

    Thanks
    Bhaven

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Before I show you how to do it, are you happy with that routine?

  5. #5
    VBAX Regular
    Joined
    Oct 2011
    Posts
    6
    Location

    Thumbs up List of files in directory by date created

    Thanks, I am happy with the routine

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    You must be running Excel 2003. Since you did not say, I used datecreated.

    [vba]Option Explicit

    Public X()
    Public i As Long
    Public objShell, objFolder, objFolderItem
    Public FSO, oFolder, Fil

    Sub MainExtractData()
    Dim NewSht As Worksheet
    Dim MainFolderName As String
    Dim TimeLimit As Long, StartTime As Double
    ReDim X(1 To 65536, 1 To 11)
    Set objShell = CreateObject("Shell.Application")
    TimeLimit = Application.InputBox("Please enter the maximum time that you wish this code to run for in minutes" & vbNewLine & vbNewLine & _
    "Leave this at zero for unlimited runtime", "Time Check box", 0)
    StartTime = Timer
    ' This is the part I need to change to create a Table with these field names.
    Application.ScreenUpdating = False
    MainFolderName = BrowseForFolder()
    Set NewSht = ThisWorkbook.Sheets.Add
    X(1, 1) = "Path"
    X(1, 2) = "File Name"
    X(1, 3) = "Last Accessed"
    X(1, 4) = "Last Modified"
    X(1, 5) = "Created"
    X(1, 6) = "Type"
    X(1, 7) = "Size"
    X(1, 8) = "Owner"
    X(1, 9) = "Author"
    X(1, 10) = "Title"
    X(1, 11) = "Comments"
    ' i is defined Publicly as a Long Integer
    i = 1
    Set FSO = CreateObject("scripting.FileSystemObject")
    Set oFolder = FSO.GetFolder(MainFolderName)
    'error handling to stop the obscure error that occurs at time when retrieving DateLastAccessed
    On Error Resume Next
    For Each Fil In oFolder.Files
    Set objFolder = objShell.Namespace(oFolder.Path)
    Set objFolderItem = objFolder.ParseName(Fil.Name)
    If Fil.datecreated >= DateSerial(2011, 1, 8) and Fil.datecreated <= DateSerial(2011, 10, 31) Then
    i = i + 1
    If i Mod 20 = 0 And TimeLimit <> 0 And Timer > (TimeLimit * 60 + StartTime) Then
    GoTo FastExit
    End If
    If i Mod 50 = 0 Then
    Application.StatusBar = "Processing File " & i
    DoEvents
    End If
    X(i, 1) = oFolder.Path
    X(i, 2) = Fil.Name
    X(i, 3) = Fil.DateLastAccessed
    X(i, 4) = Fil.DateLastModified
    X(i, 5) = Fil.datecreated
    X(i, 6) = Fil.Type
    X(i, 7) = Fil.Size
    X(i, 8) = objFolder.GetDetailsOf(objFolderItem, 8)
    X(i, 9) = objFolder.GetDetailsOf(objFolderItem, 9)
    X(i, 10) = objFolder.GetDetailsOf(objFolderItem, 10)
    X(i, 11) = objFolder.GetDetailsOf(objFolderItem, 14)
    End If
    Next
    'Get subdirectories
    If TimeLimit = 0 Then
    Call RecursiveFolder(oFolder, 0)
    Else
    If Timer < (TimeLimit * 60 + StartTime) Then Call RecursiveFolder(oFolder, TimeLimit * 60 + StartTime)
    End If
    FastExit:
    Range("A:K") = X
    If i < 65535 Then Range(Cells(i + 1, "A"), Cells(65536, "A")).EntireRow.Delete
    Range("A:K").WrapText = False
    Range("A:K").EntireColumn.AutoFit
    Range("1:1").Font.Bold = True
    Rows("2:2").Select
    ActiveWindow.FreezePanes = True
    Range("a1").Activate
    Set FSO = Nothing
    Set objShell = Nothing
    Set oFolder = Nothing
    Set objFolder = Nothing
    Set objFolderItem = Nothing
    Set Fil = Nothing
    Application.StatusBar = ""
    Application.ScreenUpdating = True
    End Sub

    Sub RecursiveFolder(xFolder, TimeTest As Long)
    Dim SubFld
    For Each SubFld In xFolder.SubFolders
    Set oFolder = FSO.GetFolder(SubFld)
    Set objFolder = objShell.Namespace(SubFld.Path)
    For Each Fil In SubFld.Files
    Set objFolder = objShell.Namespace(oFolder.Path)
    'Problem with objFolder at times
    If Not objFolder Is Nothing Then
    Set objFolderItem = objFolder.ParseName(Fil.Name)
    i = i + 1
    If i Mod 20 = 0 And TimeTest <> 0 And Timer > TimeTest Then
    Exit Sub
    End If
    If i Mod 50 = 0 Then
    Application.StatusBar = "Processing File " & i
    DoEvents
    End If
    X(i, 1) = SubFld.Path
    X(i, 2) = Fil.Name
    X(i, 3) = Fil.DateLastAccessed
    X(i, 4) = Fil.DateLastModified
    X(i, 5) = Fil.datecreated
    X(i, 6) = Fil.Type
    X(i, 7) = Fil.Size
    X(i, 8) = objFolder.GetDetailsOf(objFolderItem, 8)
    X(i, 9) = objFolder.GetDetailsOf(objFolderItem, 9)
    X(i, 10) = objFolder.GetDetailsOf(objFolderItem, 10)
    X(i, 11) = objFolder.GetDetailsOf(objFolderItem, 14)
    Else
    Debug.Print Fil.Path & " " & Fil.Name
    End If
    Next
    Call RecursiveFolder(SubFld, TimeTest)
    Next
    End Sub

    Function BrowseForFolder(Optional OpenAt As Variant) As Variant
    'Function purpose: To Browser for a user selected folder.
    'If the "OpenAt" path is provided, open the browser at that directory
    'NOTE: If invalid, it will open at the Desktop level
    Dim ShellApp As Object
    'Create a file browser window at the default folder
    Set ShellApp = CreateObject("Shell.Application"). _
    BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
    'Set the folder to that selected. (On error in case cancelled)
    On Error Resume Next
    BrowseForFolder = ShellApp.self.Path
    On Error GoTo 0
    'Destroy the Shell Application
    Set ShellApp = Nothing
    'Check for invalid or non-entries and send to the Invalid error
    'handler if found
    'Valid selections can begin L: (where L is a letter) or
    '\\ (as in \\servername\sharename. All others are invalid
    Select Case Mid(BrowseForFolder, 2, 1)
    Case Is = ":"
    If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
    Case Is = "\"
    If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
    Case Else
    GoTo Invalid
    End Select
    Exit Function
    Invalid:
    'If it was determined that the selection was invalid, set to False
    BrowseForFolder = False
    End Function

    [/vba]

  7. #7
    VBAX Regular
    Joined
    Oct 2011
    Posts
    6
    Location

    List of files in directory by date created

    Hi,

    Yes I am using excel 2003 and i wanted files by date created between 01.08.2011 to 31.10.2011.

    I have tried amended code but it extarcts all the files created in the folder including 01.08.2011 to 31.10.2011.

    I only want extact files created between 01.08.2011 to 31.10.2011

    Thanks in advance for your help

  8. #8
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    There is a lot of redundancy in that code. Try this modification where I added the IF to the recursive routine as well.

  9. #9
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi there,

    When you are reading through or stepping through the code, you'll see the test that decides which files' info to return.
    [VBA]If Fil.datecreated >= DateSerial(2011, 1, 8) And Fil.datecreated <= DateSerial(2011, 10, 31) Then[/VBA]
    Since you only want if the created date if greater than one date and less than, try changing the greater than or equal to test (>=) to simply greater than (>); then the same for the less than or equal to test, just removing the equals sign.

  10. #10
    VBAX Regular
    Joined
    Oct 2011
    Posts
    6
    Location

    List of files in directory by date created

    Hi,

    I tried your suggestion with no success

  11. #11
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    When you tried the code in my post #6, did you confirm the date created values manually?

    That is such unwieldy code, I think it best to make it better.

  12. #12
    VBAX Regular
    Joined
    Oct 2011
    Posts
    6
    Location

    List of files in directory by date created

    Hi,

    I tried your suggestion from #6 but gives same results.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •