Without using the File System Object, how can I get the size of a file?
Printable View
Without using the File System Object, how can I get the size of a file?
Private Const OPEN_EXISTING = 3
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type BY_HANDLE_FILE_INFORMATION
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
dwVolumeSerialNumber As Long
nFileSizeHigh As Long
nFileSizeLow As Long
nNumberOfLinks As Long
nFileIndexHigh As Long
nFileIndexLow As Long
End Type
Private Declare Function GetFileInformationByHandle Lib "kernel32" (ByVal hFile As Long, lpFileInformation As BY_HANDLE_FILE_INFORMATION) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Sub Command1_Click()
Dim hFile As Long
Dim FileInfo As BY_HANDLE_FILE_INFORMATION
'create a handle to the file 'c:\autoexec.bat'
hFile = CreateFile("c:\program files\MyFile.txt", 0, 0, ByVal 0&, OPEN_EXISTING, 0, ByVal 0&)
GetFileInformationByHandle hFile, FileInfo
'close the handle
CloseHandle hFile
MsgBox "File size: " + CStr(FileInfo.nFileSizeLow), vbInformation
End Sub[/Highlight]
Argggggg....I hate it when I mess up the code tags. Sorry about the AttnSue.
Or you could use the FileLen() function ;)VB Code:
Private Sub Command1_Click() Caption = FileLen("C:\SomeFile.txt") End Sub