Date: 02-16-2022
Return to Index
created by gbSnippets
'There are several methods to getting the size of a file - use of the
'LOF function, which returns the byte size of an OPENed file, the DIR$
'statements, or API (GetFileSizeEx, FindFirstFile)
'Primary Code:
'Example #1 - file already OPENed, use LOF
FileSize& = Lof(1)
'Example#2 - file not currently OPENed, open it and use LOF
Function GetSizeOfFile(sFileName$) as Long
Open sFileName$ For Binary as #1
Function = Lof(1)
Close #1
End Function
'Example#3 - use DIR$ to capture filesize info in DirData structure
Function GetSizeofFile(sFileName$) as Long
Local FileInfo As DirData, temp$
temp$ = Dir$ (sFileName$ To FileInfo)
Function = FileInfo.FileSizeHigh * &H100000000 + FileInfo.FileSizeLow
End Function
'Example#4 - use FindFirstFile to Extract File Size Information
'Credit: Semen Matusovski
Function GetSizeOfFile(sFileName$) As Long
Local FindData As WIN32_FIND_DATA, hFile As Long, sFile as Asciiz*%Max_Path
sFile = SFileName$
hFile = FindFirstFile(sFile, FindData)
If hFile = %INVALID_HANDLE_VALUE Then
Function = -1
Else
Function = FindData.nFileSizeHigh * &H100000000 + FindData.nFileSizeLow: _
FindClose hFile
End If
End Function
'Example#5 - use GetFileSizeEx API
'I can't get this one to work, so I'm leaving it here to remind me to figure out why!
'Function GetSizeOfFile(sFileName As Asciiz * %Max_Path) As Long
'Local qfs AS QUAD, hFile as Long
'hFile = CreateFile(sFileName , %GENERIC_READ Or %GENERIC_WRITE , %FILE_SHARE_READ Or %FILE_SHARE_WRITE, ByVal 0&, %OPEN_ALWAYS, %FILE_ATTRIBUTE_NORMAL, 0)
'GetFileSizeEx(hFile, qfs)
'qfs = GetFileSize(hFile, ByVal VarPTR(qfs) +2)
'End Function
'Example#6 - File not found should return a different value than a zero length file
'Credit: Tom Hanlin and Mike Doty
Function FileSize (ByVal sFileName AS String) AS QUAD
Local ddFile AS DIRDATA
Local qSize AS QUAD
If Len(Dir$(sFileName TO ddFile)) Then
Dir$ Close
qSize = ddFile.FileSizeHigh
Shift LEFT qSize, 32
qSize Or= ddFile.FileSizeLow
Function = qSize
Else
Function = -1
End If
End Function
'Compilable Example: (Jose Includes)
'This example uses method 2-4, showing the result for each in successive MsgBox's
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As DWord
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Push", 50,10,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
Local filespec$
filespec$ = "myfile.txt"
MsgBox Str$(GetSizeOfFileA (filespec$))
MsgBox Str$(GetSizeOfFileB (filespec$))
MsgBox Str$(GetSizeOfFileC (filespec$))
End If
End Function
Function GetSizeOfFileA(sFileName$) as Long
Open sFileName$ For Binary as #1
Function = Lof(1)
Close #1
End Function
Function GetSizeofFileB(sFileName$) as Long
Local FileInfo As DirData, temp$
temp$ = Dir$ (sFileName$ To FileInfo)
Function = FileInfo.FileSizeHigh * &H100000000 + FileInfo.FileSizeLow
End Function
Function GetSizeOfFileC(sFileName$) As Long
Local FindData As WIN32_FIND_DATA, hFile As Long, sFile as Asciiz*%Max_Path
sFile = sFileName$
hFile = FindFirstFile(sFile, FindData)
If hFile = %INVALID_HANDLE_VALUE Then
Function = -1
Else
Function = FindData.nFileSizeHigh * &H100000000 + FindData.nFileSizeLow: _
FindClose hFile
End If
End Function
'gbs_00150
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm