Solved

Determine a file size

Posted on 2002-05-23
5
196 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:tayleeming
  • 3
5 Comments
 
LVL 1

Expert Comment

by:Freekeko
ID: 7031009
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.
-----------------------------------------------

0
 
LVL 1

Accepted Solution

by:
Freekeko earned 50 total points
ID: 7031016
-------Put this in the declare section of the form-------


Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const FO_DELETE = &H3

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

Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long

Private Declare Function MoveFile Lib "kernel32" Alias "MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String) As Long

Private Declare Function CreateFile Lib "kernel32"
Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Long, 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

------------------------------------


this the code:

Dim lngHandle As Long

lngHandle = CreateFile("C:\Divi.txt", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)


MsgBox "The size of the selected file is" + Str$(GetFileSize(lngHandle, lngLong)) + " bytes."

This will return the size of the file in Bytes
you can do conversion from Bytes to MegaBytes
Bytes = MegaBytes\1024 or 1000

well, um, hope u think u can give me alittle more points :) anyway, hope it worx.
0
 
LVL 1

Expert Comment

by:Freekeko
ID: 7031029
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 :)
0
 
LVL 22

Expert Comment

by:cookre
ID: 7031032
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))
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7031051
msgbox format$(filelen("c:\myfile.xxx")/1024/1024 ,##.0") & "Mb"
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question