gpt4 book ai didi

vba - Excel VBA 使用 FileSystemObject 列出文件最后修改日期

转载 作者:行者123 更新时间:2023-12-04 23:56:56 32 4
gpt4 key购买 nike

这是我第一次提出问题,所以希望我遵循协议(protocol)。
这是引用“获取 vba 中的子目录列表”get list of subdirs in vba .

我发现 Brett 的示例 #1 - Using FileScriptingObject 最有帮助。但是我在结果中还需要一个数据元素 (DateLastModified)。我试图修改代码,但不断收到无效的限定符错误。以下是我所做的代码修改:

  • Range("A1:C1") = Array("文件名", "路径", "上次修改日期")。
  • Do While 循环添加了这个 => Cells(i, 3) = myFile.DateLastModified。

  • 将不胜感激包括“上次修改日期”的帮助。

    这里的 Santosh 是完整的代码,带有指示修改的注释。
    Public Arr() As String
    Public Counter As Long

    Sub LoopThroughFilePaths()
    Dim myArr
    Dim i As Long
    Dim j As Long
    Dim MyFile As String
    Const strPath As String = "c:\temp\"
    myArr = GetSubFolders(strPath)
    Application.ScreenUpdating = False
    'Range("A1:B1") = Array("text file", "path")' <= orig code
    Range("A1:C1") = Array("text file", "path", "Date Last Modified") ' <= modified code
    For j = LBound(Arr) To UBound(Arr)
    MyFile = Dir(myArr(j) & "\*.txt")
    Do While Len(MyFile) <> 0
    i = i + 1
    Cells(i, 1) = MyFile
    Cells(i, 2) = myArr(j)
    Cells(i, 3) = MyFile.DateLastModified ' <= added to modify code
    MyFile = Dir
    Loop
    Next j
    Application.ScreenUpdating = True
    End Sub

    Function GetSubFolders(RootPath As String)
    Dim fso As Object
    Dim fld As Object
    Dim sf As Object
    Dim myArr

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fld = fso.GetFolder(RootPath)
    For Each sf In fld.SubFolders
    Counter = Counter + 1
    ReDim Preserve Arr(Counter)
    Arr(Counter) = sf.Path
    myArr = GetSubFolders(sf.Path)
    Next
    GetSubFolders = Arr
    Set sf = Nothing
    Set fld = Nothing
    Set fso = Nothing
    End Function

    最佳答案

    试试这个代码:

    Sub ListFilesinFolder()

    Dim FSO As Scripting.FileSystemObject
    Dim SourceFolder As Scripting.Folder
    Dim FileItem As Scripting.File

    SourceFolderName = "C:\Users\Santosh"

    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourceFolderName)

    Range("A1:C1") = Array("text file", "path", "Date Last Modified")

    i = 2
    For Each FileItem In SourceFolder.Files
    Cells(i, 1) = FileItem.Name
    Cells(i, 2) = FileItem
    Cells(i, 3) = FileItem.DateLastModified
    i = i + 1
    Next FileItem

    Set FSO = Nothing

    End Sub

    关于vba - Excel VBA 使用 FileSystemObject 列出文件最后修改日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16627441/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com