Solved

Determine a file size

Posted on 2002-05-23
5
192 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
Comment Utility
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
Comment Utility
-------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
Comment Utility
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
Comment Utility
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
Comment Utility
msgbox format$(filelen("c:\myfile.xxx")/1024/1024 ,##.0") & "Mb"
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now