Link to home
Start Free TrialLog in
Avatar of tayleeming
tayleeming

asked on

Determine a file size

Hi,

1) i wish to know how can i get size of a file that specify by user in VB,and return the size in MB unit to the user. please provide me with a working example. Thanks
Avatar of Freekeko
Freekeko

Hi, Well, easy, look, you'll need to use the API function (GetFileSize)

here's a sample code:

------------------------------------------------
Declare Function GetFileSize Lib "kernel32" Alias "GetFileSize" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long

· hFile
Specifies an open handle of the file whose size is being returned. The handle must have been created with either GENERIC_READ or GENERIC_WRITE access to the file.

· lpFileSizeHigh
Points to the variable where the high-order word of the file size is returned. This parameter can be NULL if the application does not require the high-order word.
-----------------------------------------------

ASKER CERTIFIED SOLUTION
Avatar of Freekeko
Freekeko

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
btw, in the above code, u can ignore putting the 2 functions GetFileTime, MoveFile.

n at the end of the code, u can close file handle

CloseHandle(lngHandle) 'This will close the handle of the file after finishing.

wish ya luck again :)
For a file open with a file number, use LOF(FileNumber).

For a file that's not open, use FileLen(Path).

Both return the file size in bytes as a LONG.

For example:
MsgBox("File size in MB is "+str(FileLen("c:\thingy.foo")/1000000))
Avatar of Richie_Simonetti
msgbox format$(filelen("c:\myfile.xxx")/1024/1024 ,##.0") & "Mb"