Solved

Determine a file size

Posted on 2002-05-23
5
197 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

732 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