如何取得文件名的创建时间、修改时间、大小
编号:QA001549
建立日期: 1999年8月20日 最后修改日期:2002年4月22日
所属类别:
Q
古月剑龙:
1、如何取得文件名的创建时间、修改时间?
2、怎样取得文件的大小(多少K)?
A回答:
使用FileDateTime函数获得最后修改事件。使用FileLen函数获得文件大小(字节为单位)。获得文件建立时间需要借助API函数,如:
Function CreateTime(sFilename As String) As Date
Dim hFile As Long
Dim sDate As String
' Open file
hFile = CreateFile(sFilename, GENERIC_READ, _
FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
If hFile = -1 Then RaiseError
GetFileTime hFile, ftCreateTime, ByVal 0&, ByVal 0&
' Get Create time.
FileTimeToLocalFileTime ftCreateTime, ftCreateTime
FileTimeToSystemTime ftCreateTime, stSystemTime
sDate = stSystemTime.wYear & "/" _
& stSystemTime.wMonth & "/" & stSystemTime.wDay _
& " " & stSystemTime.wHour & ":" _
& stSystemTime.wMinute & ":" & stSystemTime.wSecond
' Close file
CloseHandle hFile
CreateTime = CDate(sDate)
End Function
lwj的意见:
选择“工程|引用”,在其中可以选择Microsoft Scripting Runtime。然后你就可以使用FileSystemObject类。
例: Dim g As Object
Dim f As Variant
Set g = CreateObject("Scripting.FileSystemObject")
Set f = g.GetFile("c:\Autoexec.bat")
MsgBox vbLf & "创建时间:" & f.DateCreated & _
vbLf & "修改时间:" & f.DateLastModified & _
vbLf & "访问时间:" & f.DateLastAccessed
此问题由李海回答。
附加关键字:编程, 源程序, programming, source code, Visual Basic, VB, 磁盘、文件和目录, disk, file, fat, directory, folder。
| |
|
|
| |
|
|