【问题标题】:Adding file date created to list将创建的文件日期添加到列表中
【发布时间】:2016-02-04 15:54:59
【问题描述】:

我一直在尝试编辑一些代码,该代码列出了所有子文件夹中的所有文件,同时也给出了在下一列中创建的日期,但我不确定如何。这是我正在使用的代码:它可以很好地获取文件路径,但不能获取文件 DateCreateds

 Sub startIt()

   Dim FileSystem As Object
   Dim HostFolder As String

   HostFolder = "C:\folderthing"

   Set FileSystem = CreateObject("Scripting.FileSystemObject")
   DoFolder FileSystem.GetFolder(HostFolder)

 End Sub

 Sub DoFolder(Folder)


    Dim SubFolder
    For Each SubFolder In Folder.SubFolders
      DoFolder SubFolder
    Next

    i = Cells(Rows.Count, 1).End(xlUp).Row + 1
    Dim File
    For Each File In Folder.Files
      ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 1), Address:= _
          File.Path, TextToDisplay:=File.Path
      ActiveSheet.Add TextToDisplay:=File.DateCreated

      i = i + 1

    Next


End Sub

【问题讨论】:

  • 你试过FileDateTime(pathname)吗? MSDN link
  • 我刚刚尝试了这个确切的脚本。 ActiveSheet 行因错误而失败。 Remming出来,它工作得很好。所以我将该行更改为: Range("A1") = File.DateCreated。这给了我它读取的最后日期。我认为您的问题出在 ActiveSheet.Add 中。
  • 正如@durbnpoisn 所说,我认为您需要将这一行ActiveSheet.Add TextToDisplay:=File.DateCreated 替换为Cells(i, 2).value = File.DateCreated
  • 不管具体问题如何,这都是可用于制作音乐、图片和视频文件目录的简洁代码。

标签: vba excel macros


【解决方案1】:

如果您想要B列中的日期,则:

Sub startIt()

   Dim FileSystem As Object
   Dim HostFolder As String

   HostFolder = "C:\TestFolder"

   Set FileSystem = CreateObject("Scripting.FileSystemObject")
   DoFolder FileSystem.GetFolder(HostFolder)

 End Sub

 Sub DoFolder(Folder)


    Dim SubFolder
    For Each SubFolder In Folder.SubFolders
      DoFolder SubFolder
    Next

    i = Cells(Rows.Count, 1).End(xlUp).Row + 1
    Dim File
    For Each File In Folder.Files
      ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 1), Address:= _
          File.Path, TextToDisplay:=File.Path
      Cells(i, 2).Value = File.DateCreated

      i = i + 1

    Next


End Sub

【讨论】:

  • 天哪。这就是我要发布的确切解决方案。
  • @durbnpoisn 这并不奇怪............ IIJHFII 提出问题的方式 (代码中只有一行错误) 将推动一种常见的方法来做出答案。
  • 我只是觉得这很有趣。我做了和你一样的事情。在 Excel 中对其进行了重构,并找到了一种有效的方法。至少我现在知道 Excel 读取整个“工作”文件夹时的样子。
猜你喜欢
  • 1970-01-01
  • 2023-02-24
  • 2021-05-30
  • 2017-11-17
  • 2019-08-15
  • 1970-01-01
  • 2017-07-24
  • 2017-12-02
  • 2017-04-09
相关资源
最近更新 更多