返回列表 上一主題 發帖

[發問] 資料夾問題請益

[發問] 資料夾問題請益

本帖最後由 child 於 2011-9-28 14:35 編輯

若想列出D:\TEST裏的所有資料夾包含子資料夾的名稱和修改時間,目前測試的程式只能列出D:\TEST裏的資料夾,但無法列出子資料夾。
Ex:
D:\TEST有AA和AB........等資料夾,但AA和AB資料夾裏又有01、02........等資料夾,想將資料呈現如下:

主資料夾  子資料夾  最終修改時間
AA              01            2011/9/24  02:23:32
AA              02            2011/9/24  01:33:32
AA              03            2011/9/25  08:33:32
AB              01            2011/9/23  03:33:01
AB              02            2011/9/25  01:34:30

最終修改時間為子資料夾的時間,請益如何修改程式碼?

Sub TEST()
    Dim fs, f
    Dim i As Integer
   
    Sheets("TEST").Activate
    Range("A1:C65536").ClearContents
   
    Set fs = CreateObject("Scripting.FileSystemObject").GetFolder("D:\TEST")
      For Each f In fs.SubFolders
          i = i + 1
          Cells(i, 1).Resize(1, 2) = Array(f.Name, f.DateLastModified)
      Next
End Sub

很謝謝大家的幫忙^__^
小孩

本帖最後由 GBKEE 於 2011-9-28 15:24 編輯

回復 1# child
  1. Option Explicit
  2. Dim i As Integer
  3. Sub Ex()
  4.     Dim Fs, F
  5.     Sheets(1).Activate
  6.     Range("A1:C65536").ClearContents
  7.     i = 1
  8.     Set Fs = CreateObject("Scripting.FileSystemObject").GetFolder("D:\TEST")
  9.     Cells(i, 1).Resize(1, 2) = Array(Fs, Fs.DateLastModified)
  10.     For Each F In Fs.SubFolders
  11.          i = i + 1
  12.         Cells(i, 1).Resize(1, 2) = Array(F, F.DateLastModified)
  13.         子資料夾 F
  14.     Next
  15. End Sub
  16. Private Sub 子資料夾(TheFolder)
  17.     Dim Fs As Object, F As Object
  18.     Set Fs = CreateObject("Scripting.FileSystemObject").GetFolder(TheFolder)
  19.     For Each F In Fs.SubFolders
  20.         i = i + 1
  21.         Cells(i, 1).Resize(1, 2) = Array(F, F.DateLastModified)
  22.         子資料夾 F
  23.     Next
  24. End Sub
複製代碼

TOP

  1. Public r As Integer

  2. Sub 幫我做()
  3.    
  4.     Dim r As Integer
  5.     Dim fs As New FileSystemObject
  6.     Dim myfolder As Folder
  7.     Set myfolder = fs.GetFolder("D:\TEST")
  8.    
  9.     r = 0

  10.     列出資料夾 myfolder

  11. End Sub


  12. Sub 列出資料夾(fd As Folder)

  13.     Dim f As File
  14.     Dim sfd As Folder
  15.    
  16.     For Each sfd In fd.SubFolders
  17.         For Each f In sfd.Files
  18.             r = r + 1
  19.             Cells(r, "A") = Split(fd, "\")(2)
  20.             Cells(r, "B") = Split(sfd, "\")(3)
  21.             Cells(r, "C") = f.Name
  22.             Cells(r, "D") = f.DateLastModified
  23.         Next
  24.         列出資料夾 sfd
  25.     Next

  26. End Sub
複製代碼
diabo

TOP

回復  child
GBKEE 發表於 2011-9-28 15:12



    謝謝GBKEE版主的幫忙^__^
小孩

TOP

diabo 發表於 2011-9-28 15:13



       也謝謝diabo 的幫忙^__^
小孩

TOP

DEAR.  

可再加入,,,最終資料夾之檔案數量 ???


麻煩..

TOP

回復 6# tomking
  1. Sub Ex()
  2.     Dim Fs
  3.     Sheets(1).Activate
  4.     Range("A1:C65536").ClearContents
  5.     i = 1
  6.     Set Fs = CreateObject("Scripting.FileSystemObject").GetFolder("D:\test")
  7.     Cells(i, 1).Resize(1, 3) = Array(Fs, Fs.DateLastModified, Fs.Files.Count)
  8.     子資料夾 Fs
  9. End Sub
  10. Private Sub 子資料夾(TheFolder)
  11.     Dim Fs As Object, F As Object
  12.     Set Fs = CreateObject("Scripting.FileSystemObject").GetFolder(TheFolder)
  13.     For Each F In Fs.SubFolders
  14.         i = i + 1
  15.         Cells(i, 1).Resize(1, 3) = Array(F, F.DateLastModified, F.Files.Count)
  16.         子資料夾 F
  17.     Next
  18. End Sub
複製代碼

TOP

感謝, 版主,幫忙...   

感恩

TOP

        靜思自在 : 【時日莫空過】一個人在世間做了多少事,就等於壽命有多長。因此必須與時間競爭,切莫使時日空過。
返回列表 上一主題