Tip

Get File(Time)Information

This class gives you information about
FileSize, FileAttributes, LastAccessed, LastModified etc.

'Sample usage:

Sub Click(Source As Button)

Const LAST_ACCESS = 0
Const LAST_WRITE = 1
Const CREATION_TIME = 2
Const EIGHT_THREE_NAME = 3
Const FILE_SIZE = 4
Const FILE_NAME = 5
Const FILE_ATTRIBUTES = 6

Dim FI As New FileInfo

File$ = "c:\mpie4ful.exe"

Msgbox FI.GetFileTimeInfo ( File$ , LAST_ACCESS )
Msgbox FI.GetFileInfo ( File$ , FILE_SIZE )

End Sub
' Put this code into the declaration section

Const INVALID_HANDLE_VALUE = -1
Const MAX_PATH = 260

Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Type WIN32_FIND_DATA
dwFileAttributes As Long ' Dateiattribute
ftCreationTime As FILETIME ' Erstellungsdatum
ftLastAccessTime As FILETIME ' Letzter Zugriff
ftLastWriteTime As FILETIME ' Letzte Speicherung
nFileSizeHigh As Long ' Gr??e (Hi)
nFileSizeLow As Long ' Gr??e (Lo)
dwReserved0 As Long ' bedeutungslos
dwReserved1 As Long ' bedeutungslos
cFileName As String * MAX_PATH ' Dateiname
cAlternate As String * 14 ' 8.3-Dateiname
End Type

Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type


Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As
FILETIME, lpLocalFileTime As FILETIME) As Long
Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (Byval
lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (Byval
hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Declare Function FindClose Lib "kernel32" (Byval hFindFile As Long) As Long
Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME,
lpSystemTime As SYSTEMTIME) As Long
Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As
SYSTEMTIME, lpFileTime As FILETIME) As Long

Class FileInfo

Public Function GetFileTimeInfo ( strPath As String , MODE As Integer ) As
String

Dim FileHandle As Long
Dim FileData As WIN32_FIND_DATA
Dim LocalFileTime As FILETIME
Dim LocalSystemTime As SYSTEMTIME

FileHandle = FindFirstFile(strPath & vbNullChar, FileData)
Select Case MODE

Case 0
FileTimeToLocalFileTime FileData.ftLastAccessTime,
LocalFileTime
Case 1
FileTimeToLocalFileTime FileData.ftLastWriteTime,
LocalFileTime
Case 2
FileTimeToLocalFileTime FileData.ftCreationTime,
LocalFileTime

End Select

FileTimeToSystemTime LocalFileTime, LocalSystemTime

GetFileTimeInfo = _
Left$(FileData.cFileName, Instr(FileData.cFileName, vbNullChar) - 1)
_
& LocalSystemTime.wDay & "." _
& LocalSystemTime.wMonth & "." _
& LocalSystemTime.wYear & ", "_
& LocalSystemTime.wHour & ":" _
& Format$(LocalSystemTime.wMinute, "00") _
& ":" & Format$(LocalSystemTime.wSecond, "00") & " Uhr"

FindClose FileHandle

End Function

Public Function GetFileInfo ( strPath As String , MODE As Integer ) As
String

Dim FileHandle As Long
Dim FileData As WIN32_FIND_DATA

FileHandle = FindFirstFile ( strPath & vbNullChar , FileData )

If FileHandle <> INVALID_HANDLE_VALUE Then

Select Case MODE

Case 3
GetFileInfo = FileData.cFileName
Case 4
GetFileInfo = Cstr(FileData.nFileSizeLow)
Case 5
GetFileInfo = FileData.cFileName
Case 6
GetFileInfo = Cstr(FileData.dwFileAttributes)

End Select
End If
FindClose FileHandle

End Function

End Class

This was first published in November 2000

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.