leonchou
論壇維護群
離線
文章: 1204
|
一. 取得檔案日期相關屬性 1. 使用 FileSystem object --
Function FileDate(filespec, datetype) Dim fs, f, s Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.GetFile(filespec) Select Case datetype Case 1: FileDate = f.DateCreated '檔案建立日期 Case 2: FileDate = f.DateLastAccessed '存取日期 Case 3: FileDate = f.DateLastModified '修改日期 End Select End Function
使用範例 -- 在工作輸入 =FILEDATE("d:\temp\1115p.zip",2) 得到指定檔案的最後存取時間, 代碼可容許 1, 2, 3 使用該函數的儲存格要設成日期時間格式.
2. 使用VBA內建函數FileDateTime -- 例 Msgbox FileDateTime("D:\Temp\xyz.txt")
二. 取得目錄下的檔案數量 方法1: Dir + Loop pp = "D:\Temp" fi = Dir(pp & "\prn*.tmp") Do While fi <> "" ff = ff + 1 fi = Dir Loop MsgBox pp & " 找到 " & ff & " 個檔案!"
方法2: FileSearch 物件 pp = "D:\Temp": ff = 0 With Application.FileSearch .NewSearch .LookIn = pp .Filename = "prn*.tmp" If .Execute() > 0 Then ff = .FoundFiles.Count End With MsgBox pp & " 找到 " & ff & " 個檔案!"
方法3: FileSystem + GetFolder Set fs = CreateObject("Scripting.FileSystemObject") pp = "D:\Temp" ff = fs.GetFolder(pp).Files.Count MsgBox pp & " 資料夾有 " & ff & " 個檔案!"
PS. 方法3 只能找某特定資料夾內所有檔案
|