0

i want to import the whole path of the file C:\Users\fld but just the recent files in today for example .

Can you help me please ?

below my vba to import the path:

Sub test()
      Dim fso As FileSystemObject
      Dim oSourceFolder As Scripting.Folder
      Dim oSubFolder As Scripting.Folder
      Dim oFile As Scripting.File
      Dim oFolder As Scripting.Folder
      Dim strFolderName As String
      Dim i As Long
        Set fso = CreateObject("Scripting.FileSystemObject")
        Cells(1, 1).Value = "fld"   
       strFolderName= "C:\Users\fld"

        i = 2      
        Range("A2").Select
        ActiveCell = Range("A2")   
      Set oSourceFolder = fso.GetFolder(strFolderName)
      For Each oFolder In oSourceFolder.SubFolders
                For Each oFile In oFolder.Files
                Cells(i, 1).Value = Left(oFile.ParentFolder)                
                             i = i + 1
                Next oFile
        Next oFolder
mitsuki
  • 1
  • 2
  • 7
  • Do you mean that you want to import the file paths for all files in a given folder structure (including subfolders) and write them into column A on your spreadsheet? – Dave May 12 '16 at 14:46
  • I believe that you can find the solution here: http://stackoverflow.com/questions/17847457/getting-file-last-modified-date-explorer-value-not-cmd-value – Ralph May 12 '16 at 15:15

1 Answers1

0

Try this :

Sub test()
    Dim fso As FileSystemObject
    Dim oSourceFolder As Scripting.Folder
    Dim oSubFolder As Scripting.Folder
    Dim oFile As Scripting.File

    Dim oFolder As Scripting.Folder
    Dim strFolderName As String
    Dim i As Long
    Set fso = CreateObject("Scripting.FileSystemObject")
    strFolderName = "C:\Users\fld"
    i = 2
    Range("A2").Select
    ActiveCell = Range("A2")

    Dim dt As Date
    Dim pathLastUpdatePath As String

    'Find last Date of file----------------------------------------'
    'Find file that have last DateTime Update in parent folder
    Set oFolder = fso.GetFolder(strFolderName)
    For Each oFile In oFolder.Files
        fileModDate = oFile.DateLastModified
        If fileModDate > dt Then
            dt = fileModDate
            pathLastUpdatePath = oFile.Path
        End If
    Next oFile

    'Find file that have last DateTime Update in Sub folders
    Set oSourceFolder = fso.GetFolder(strFolderName)
    For Each subFolder In oSourceFolder.SubFolders
        For Each sFile In subFolder.Files
            fileModDate = sFile.DateLastModified
            If fileModDate > dt Then
                dt = fileModDate
                pathLastUpdatePath = sFile.Path
            End If
        Next sFile
    Next subFolder

    'Find and Show all files are modified in the same day----------------------'
    Cells(1, 1).Value = "File with DateLastModified"
    Cells(1, 2).Value = "DateTime File"

    Set oFolder = fso.GetFolder(strFolderName)
    For Each oFile In oFolder.Files
        fileModDate = oFile.DateLastModified
        If DateValue(fileModDate) = DateValue(dt) Then
            Cells(i, 1).Value = oFile.Path
            Cells(i, 2).Value = fileModDate
            i = i + 1
        End If
    Next oFile

    Set oSourceFolder = fso.GetFolder(strFolderName)
    For Each subFolder In oSourceFolder.SubFolders
        For Each sFile In subFolder.Files
            fileModDate = sFile.DateLastModified
            If DateValue(fileModDate) = DateValue(dt) Then
                Cells(i, 1).Value = sFile.Path
                Cells(i, 2).Value = fileModDate
                i = i + 1
            End If
        Next sFile
    Next subFolder
End Sub
Abdellah OUMGHAR
  • 3,627
  • 1
  • 11
  • 16