file check?

deathman5
09-07-2003, 08:44 AM
How can I say if a file exists do that
I mean I wanna ask if c:\hello.txt exists
form1.hide
form2.show
end if

how to say that?

Another thing, can I tell the program, on the first lanch( the first time the user starts the program) msgbox "This is the first time"
but on the second time or more dont msgbox...
or anything similar.

Thnaks:)

sPiKie
09-07-2003, 08:54 AM
I made a simple function here..:

Function FileExists(sPath As String) As Boolean
If Dir(sPath) > 0 Then
FileExists = True 'it exist
Else
FileExists = False 'it doesnt exist
End if
End Function

You can simple use it like this:

If FileExist("C:\myfile.exe") Then
'it exist
Else
'it doesnt exist
End If

:)

sPiKie
09-07-2003, 08:55 AM
The second question can you use SaveSetting() and GetSetting().

reboot
09-07-2003, 09:50 AM
If Dir$("C:\myfile.exe") Then
'it exists
Else
'it doesnt exist
End If

deathman5
09-09-2003, 04:24 AM
@sPiKie
so it should be

Private Sub Command1_Click()
Function FileExists(sPath As String) As Boolean
If Dir(sPath) > 0 Then
FileExists = True 'it exist
Else
FileExists = False 'it doesnt exist
End If
End Function


If FileExist("C:\myfile.exe") Then
Label1.Caption = "It exists"
Else
Label1.Caption = "it doesnt exist"
End If
End Function
End Function

??
or what?

@reboot
it says type mismatch
at

If Dir$("C:\myfile.exe") Then

deathman5
09-09-2003, 04:25 AM
plus can anybody explain more about SaveSetting() and GetSetting() please
thanks alot :)

Garmour
09-09-2003, 04:35 AM
Reboot may have meant

if dir$("c:\myfile.exe")="" then
'it exists
else
'it doesn't exist
endif


Bascially the dir$ command will return a string. The string will be the name of the file you are looking for (if it exists) or and empty string (if it doesn't)

Garmour
09-09-2003, 04:36 AM
Both SaveSetting and GetSetting are in the VB help files (with examples of usage)

reboot
09-09-2003, 08:01 AM
Garmour, thanks. That is indeed what I meant. :)

Fred_Leonard
09-09-2003, 07:27 PM
Actually the function doesn't work.

If the path is bad, say not connected to the network, CD removed, etc, and an error is generated, then the application will stop.

Also, the logic was backwards. (<>"", not ="")

Here's my version that works:

Function fExists(sPath as string) as boolean
on local error GOTO fNotExists
if dir$(sPath)<>"" then
fExists=True
exit function
end if
fNotExists:
fExists=False
End Function

reboot
09-09-2003, 07:41 PM
I still don't understand why people keep writing functions when Dir$ does just fine by itself (and the functions all use Dir$).

Val
09-09-2003, 07:46 PM
Please test the string you pass to dir for being null, because if you pass a null string to dir, it [sometimes] will return the first file name in a current folder.

Function FileExists(ByVal FileName As String) As Boolean
'If filename is null, exit returning false
If Len(FileName) = 0 Then Exit Function
'Same as <> "", but Len is faster.
FileExists = Len(Dir(FileName))
End Function

Fred_Leonard
09-10-2003, 01:25 PM
I still don't understand why people keep writing functions when Dir$ does just fine by itself (and the functions all use Dir$).

For the same reason as described in my function right above your post.

It doesn't work unless you trap the errors. This is because a path may be bad or inaccessible, like a disconnected network path, removed CD/DVD, etc.


Function fExists(sPath as string) as boolean
on local error GOTO fNotExists
if dir$(sPath)<>"" then
fExists=True
exit function
end if
fNotExists:
fExists=False
End Function

reboot
09-10-2003, 01:34 PM
A simple On Error Resume Next takes care of that. *shrug*

Fred_Leonard
09-10-2003, 03:15 PM
A simple On Error Resume Next takes care of that. *shrug*

Yeah, but when you're writing a lot of code, do you really want to put on error resume next all through your app?

With a simple function like this, you just get a true/false in one statement with one local error trap.

I guess if you're only going to do it once, it wouldn't matter.

fExists really is a necessary function, and I never could figure out why it wasn't included in VB, especially when just about every other language out there has this function. (even Kix)

SnakeChomp
09-10-2003, 03:19 PM
lol! VB DOES have an fExists function, and its called Dir$. In case you didnt notice, you USE Dir$ In your so called vital function. All your function is is a wrapper for the Dir$ function. You also can have multiple levels of error trapping within the same function. Put on error resume next before Dir$, and restore what ever error handling you used to have afterwards.

reboot
09-10-2003, 03:21 PM
I've grown tired of trying to make that point. :)

Agent707
09-10-2003, 03:34 PM
Please test the string you pass to dir for being null, because if you pass a null string to dir, it [sometimes] will return the first file name in a current folder.

Function FileExists(ByVal FileName As String) As Boolean
'If filename is null, exit returning false
If Len(FileName) = 0 Then Exit Function
'Same as <> "", but Len is faster.
FileExists = Len(Dir(FileName))
End Function

When you supply DIR() with a path to a folder only - it returns the first file in the directory, as long as the folder isn't empty.

When you supply DIR(path + filename), it returns that filename Only, or nothing if the file/path doesn't exist.

If you give it a Path to a folder that has NO FILES in it, it will return nothing as well.

Also, when you pass a blank("", not Null) to Dir, is uses app.path as the path to look in.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum