Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA使用FileSystemObject列出上次修改的文件日期_Vba - Fatal编程技术网

Excel VBA使用FileSystemObject列出上次修改的文件日期

Excel VBA使用FileSystemObject列出上次修改的文件日期,vba,Vba,这是我第一次问这个问题,所以我希望我遵守协议。 这是参考“获取vba中的子曲面列表” 我发现Brett的示例#1-使用FileScript对象非常有用。但在结果中还需要一个数据元素(DateLastModified)。我试图修改代码,但不断得到无效的限定符错误。以下是我所做的代码修改: 范围(“A1:C1”)=数组(“文件名”、“路径”、“上次修改日期”) Do While循环添加了this=>Cells(i,3)=myFile.DateLastModified 将“上次修改日期”包括在内将不胜

这是我第一次问这个问题,所以我希望我遵守协议。 这是参考“获取vba中的子曲面列表”

我发现Brett的示例#1-使用FileScript对象非常有用。但在结果中还需要一个数据元素(DateLastModified)。我试图修改代码,但不断得到无效的限定符错误。以下是我所做的代码修改:

  • 范围(“A1:C1”)=数组(“文件名”、“路径”、“上次修改日期”)
  • Do While循环添加了this=>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  
    
    Public Arr()作为字符串
    公共柜台只要
    子循环通过文件路径()
    暗迈尔
    我想我会坚持多久
    Dim j尽可能长
    将MyFile设置为字符串
    Const strPath As String=“c:\temp\”
    myArr=GetSubFolders(strPath)
    Application.ScreenUpdating=False
    '范围(“A1:B1”)=数组(“文本文件”、“路径”)'请尝试以下代码:

    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
    

    你能把完整的代码和你所做的修改放在一起吗?下面是完整的代码和修改