Article ID 1184 Article Title HOWTO: Get information about a disk file using the Windows API Article URL http://www.dataaccess.com/kbasepublic/KBPrint.asp?ArticleID=1184 KBase Category Visual DataFlex Date Created 11/14/2000 Last Edit Date 05/24/2001
Article Text
QUESTION:
How can I retrieve the size of a file on a disk?
ANSWER:
You can use the Windows API to get this type of information. Using the attached code, you can a file's size and time/date, and many more attributes.
This is the Windows API function call code, and attached you can find a complete compileable example:
Define INVALID_HANDLE_VALUE For |CI-1
Define INVALID_FILE_SIZE For |CI$FFFFFFFF
Define ERROR_NO_MORE_FILES For |CI18
Define Max_Path For |CI260
Define MinChar For |CI$80
Define MaxChar For |CI$7F
Define MinShort For |CI$8000
Define MaxShort For |CI$7FFF
Define MinLong For |CI$80000000
Define MaxLong For |CI$7FFFFFFF
Define MaxByte For |CI$FF
Define MaxWord For |CI$FFFF
Define MaxDword For |CI$FFFFFFFF
// For example, to get the time string
// "11:29:40 PM"
// use the following picture string:
// "hh':'mm':'ss tt"
External_Function GetTimeFormat "GetTimeFormatA" Kernel32.Dll ;
Dword LCID Dword dwFlags Pointer lpsSystemTime Pointer lpFormat Pointer lpTimeStr Integer cchTime Returns Integer
// This function returns the time for the computer. It is the time of the clock shown in the taskbar of windows
External_Function GetLocalTime "GetLocalTime" Kernel32.Dll ;
Pointer lpsSystemTime Returns Integer
// This function returns the time in Greenwich (GST) currently based on the local time and the timezone information
External_Function GetSystemTime "GetSystemTime" Kernel32.Dll ;
Pointer lpsSystemTime Returns Integer
//External_Function GetLastError "GetLastError" Kernel32.Dll ;
// Returns Dword
External_Function GetDateFormat "GetDateFormatA" Kernel32.Dll ;
Dword LCID Dword dwFlags Pointer lpsSystemTime Pointer lpFormat Pointer lpDateStr Integer cchDate Returns Integer
Define LOCALE_NOUSEROVERRIDE For |CI$80000000 // do not use user overrides
Define TIME_NOMINUTESORSECONDS For |CI$00000001 // do not use minutes or seconds
Define TIME_NOSECONDS For |CI$00000002 // do not use seconds
Define TIME_NOTIMEMARKER For |CI$00000004 // do not use time marker
Define TIME_FORCE24HOURFORMAT For |CI$00000008 // always use 24 hour format
// Date Flags for GetDateFormatW.
Define DATE_SHORTDATE For |CI$00000001 // use short date picture
Define DATE_LONGDATE For |CI$00000002 // use long date picture
Define DATE_USE_ALT_CALENDAR For |CI$00000004 // use alternate calendar (if any)
// declare required structures
Type SystemTime
Field SystemTime.wYear As Word
Field SystemTime.wMonth As Word
Field SystemTime.wDayOfWeek As Word
Field SystemTime.wDay As Word
Field SystemTime.wHour As Word
Field SystemTime.wMinute As Word
Field SystemTime.wSecond As Word
Field SystemTime.wMilliseconds As Word
End_Type
Type FileTime
Field FileTime.dwLowDateTime As Dword
Field FileTime.dwHighDateTime As Dword
End_Type
Type Win32_Find_Data
Field Win32_Find_Data.dwFileAttributes As Dword
Field Win32_Find_Data.ftCreationLowDateTime As Dword
Field Win32_Find_Data.ftCreationHighDateTime As Dword
Field Win32_Find_Data.ftLastAccessLowDateTime As Dword
Field Win32_Find_Data.ftLastAccessHighDateTime As Dword
Field Win32_Find_Data.ftLastWriteLowDateTime As Dword
Field Win32_Find_Data.ftLastWriteHighDateTime As Dword
Field Win32_Find_Data.nFileSizeHigh As Dword
Field Win32_Find_Data.nFileSizeLow As Dword
Field Win32_Find_Data.dwReserved0 As Dword
Field Win32_Find_Data.dwReserved1 As Dword
Field Win32_Find_Data.cFileName As Char Max_Path
Field Win32_Find_Data.cAlternateFileName As Char 14
End_Type
// lpFileTime :pointer to file time to convert
// lpSystemTime :pointer to structure to receive system time
External_Function FileTimeToSystemTime "FileTimeToSystemTime" Kernel32.Dll ;
Pointer lpFileTime Pointer lpSystemTime Returns Integer
External_Function FileTimeToLocalFileTime "FileTimeToLocalFileTime" Kernel32.Dll ;
Pointer lpFileTime Pointer lpSystemTime Returns Integer
// lpFileName :address of name of file to search for
// lpFindFileData :address of returned information
External_Function FindFirstFile "FindFirstFileA" Kernel32.Dll ;
Pointer lpFileName Pointer lpFindFileData Returns Handle
// hFindFile :handle of search
// lpFindFileData :address of structure for data on found file
External_Function FindNextFile "FindNextFileA" Kernel32.Dll ;
Handle hFindFile Pointer lpFindFileData Returns Integer
// hFindFile :file search handle
External_Function FindClose "FindClose" Kernel32.Dll ;
Handle hFindFile Returns Integer
EXAMPLE:
To get the file size of a single file, you could use this code:
Function APIFileSize Global string sFileName returns integer
dWord dwFileSizeHigh dwFileSizeLow
integer iFileSize iVoid
handle hFindFile
pointer lpsFilePath lpsWin32FindData
string sWin32FindData
GetAddress of sFileName to lpsFilePath
ZeroType Win32_Find_Data to sWin32FindData
GetAddress of sWin32FindData to lpsWin32FindData
move (FindFirstFile (lpsFilePath, lpsWin32FindData)) to hFindFile
if (hFindFile<>INVALID_HANDLE_VALUE) begin
GetBuff From sWin32FindData At Win32_Find_Data.nFileSizeHigh To dwFileSizeHigh
GetBuff From sWin32FindData At Win32_Find_Data.nFileSizeLow To dwFileSizeLow
end
move (FindClose (hFindFile)) to iVoid
moveInt ((dwFileSizeHigh * MaxDword) + dwFileSizeLow) to iFileSize
function_return iFileSize
End_Function // APIFileSize
Links Related to this Article
File directory_browser.src
Email this Article
Email this Article to a Colleague
Send Feedback on this Article to Data Access Worldwide
Copyright ©2024 Data Access Corporation. All rights reserved.
The information provided in the Data Access Technical Knowledge Base is provided "as is" without warranty of any kind. Data Access Corporation disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. In no event shall Data Access Corporation or its suppliers be liable for any damages whatsoever including direct, indirect, incidental, consequential, loss of business profits or special damages, even if Data Access Corporation or its suppliers have been advised of the possibility of such damages. Some states do not allow the exclusion or limitation of liability for consequential or incidental damages so the foregoing limitation may not apply.