Solved

Determine a file size

Posted on 2002-05-23
5
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

728 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