【问题标题】:Last modified date, folder path in a cell上次修改日期,单元格中的文件夹路径
【发布时间】:2013-10-17 12:44:03
【问题描述】:

我有一个复杂的问题,解释起来比较复杂,但让我开始吧。

我在 excel 单元格中有一条路径(路径导致共享),我想在 VBA 中进行。

我想获取上次修改日期最短的文件(文件最后在那个文件夹中修改),但不确定我正在扫描的文件夹有多少子文件夹。

我的问题是,如何扫描文件夹以查找文件的上次修改日期以及如何扫描该文件夹的子文件夹(如果有子文件夹)?

【问题讨论】:

    标签: excel vba directory last-modified


    【解决方案1】:

    试试这个代码:

    Option Explicit
    
    Public newestFile As Object
    
    Sub start()
      Call getNewestFile("C:\yourpath")
      MsgBox newestFile.Name
    End Sub
    
    Private Sub getNewestFile(folderPath As String)
        Dim objFSO As Object
        Dim objFolder As Object
        Dim objFile As Object
    
        'get the filesystem object from the system
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFolder = objFSO.GetFolder(folderPath)
    
    
      'go through the subfolder and call itself
      For Each objFile In objFolder.SubFolders
        Call getNewestFile(objFile.Path)
      Next
    
      For Each objFile In objFolder.Files
        If newestFile Is Nothing Then
          Set newestFile = objFile
        ElseIf objFile.DateLastModified > newestFile.DateLastModified Then
          Set newestFile = objFile
        End If
      Next
    End Sub
    

    【讨论】:

    • 哇,谢谢!我认为这正是我在过去几天试图做的。我还有一个问题,我使用的路径在服务器上(\\server\..)我不确定这是否是它无法正常工作的原因。
    • 对我来说,如果我使用像“\\server\folder\..”这样的路径,它会起作用你收到任何错误消息吗?
    • 是的,它现在可以工作,我的权限有问题(2admin 帐户,它们都没有工作。现在可以工作了!:))用于在单元格中写入 lastmodifieddate 以及名称,我可以只需添加 objFile.FileDateTime,对吗?
    猜你喜欢
    • 2016-02-01
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    相关资源
    最近更新 更多