Using Apis

S

Silvester

Hi,

Before starting my A2000 app I would like to verify
1. existence of myvital.dll
2. filesize of myvital.dll

How can I code this using apis ?
 
L

Lowest of the Low

Is there a reason that you need to use the API's?

To find if the file exists you can use the dir function e.g. if
len(dir("C:\myvital.dll")) then .... else .... End if

Unfortunately I don't know of any inbuilt functions for determining the size
of file but you can use the FileSystemObject (FSO) in the Scripting Runtime
to do this for you, if your using the FSO for the size then you can also use
it for checking if the file exists:

Dim oFSO As FileSystemObject
Dim oFile As File
Dim sPath As String

Set oFSO = New FileSystemObject

sPath = "C:\myvital.dll"

If oFSO.FileExists(sPath) Then
Set oFile = oFSO.GetFile(sPath)
MsgBox oFile.Size
Else
MsgBox ("File missing")
End If

If you still want to use API's then your best bet is FindFirstFile and
GetFileSize(Win 95,98,Me, NT 3.51) or GetFileSizeEX (Win 2k, XP)
 
N

Nikos Yannacopoulos

Silvester,

The following function in Access VB will return the
filesize if the file exists, or -1 if it does not. You van
pass the file name (with full path) as a parameter as in
the example, or change the function to hardcode it if it
never changes.

Function filesize(fname As String)
Dim fs, f

If Dir(fname) <> "" Then
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(fname)
filesize = f.Size
Set f = Nothing
Set fs = Nothing
Else
filesize = -1
End If

End Function

HTH,
Nikos
 
S

Silvester

Thanks Nikos,
That worked great !

Nikos Yannacopoulos said:
Silvester,

The following function in Access VB will return the
filesize if the file exists, or -1 if it does not. You van
pass the file name (with full path) as a parameter as in
the example, or change the function to hardcode it if it
never changes.

Function filesize(fname As String)
Dim fs, f

If Dir(fname) <> "" Then
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(fname)
filesize = f.Size
Set f = Nothing
Set fs = Nothing
Else
filesize = -1
End If

End Function

HTH,
Nikos
 
D

Douglas J. Steele

Despite what the other posters have said, it's not necessary to use FSO to
do what you're trying to do (and, in my opinion, it's not desirable to use
FSO unless you must: it introduces unnecessary overhead, and there can be
versioning problems)

Here's Nikos's function rewritten without using FSO

Function filesize(fname As String) As Long

If Len(Dir(fname)) > 0 Then
filesize = FileLen(fname)
Else
filesize = -1
End If

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top