Check if file exists or not

Ripley

Registered User.
Local time
Today, 15:22
Joined
Aug 4, 2006
Messages
148
How would i go about checking to see is a file at a given path exists or not?

I want to say: (for example)

Code:
IF "C:\Test File.txt" exists THEN
Kill "C:\Test File.txt"
Else
Exit Sub

Thanks for your help.
 
Code:
IF Dir("C:\Test File.txt") > 0 THEN
Kill "C:\Test File.txt"
Else
Exit Sub
 
Nope dosent seem to work!

Here is the code i have, i've changed it slightly, i have a message box on the form where the user can type in the path.

I want to check for the existance of text files, so i have:

Code:
Private Sub Command27_Click()
If Dir(clientspath & "*.txt") = > 0 Then
Kill clientspath & "*.txt"
Else
Exit Sub
End If
End Sub

I get a "Run-Time Error 3" saying there's a type mis-match.
 
Try
Code:
dim FileExistsbol as boolean
FileExistsbol = Dir(C:\Test File.txt) <> vbNullString
if FileExistsbol then Kill "C:\Test File.txt"

The syntax dir(filname) returns a string value not a numeric value.
 
ahh i didn't think of using a boolean dimension, thanks for your help!
 
Additional Thought

Thank RuralGuy. I was faced with this very problem three weeks ago and I ran accross his answer. So I am just passing it on.


PS: For my code to work I had to use the TRIM function. It took me a while to figure that one out.
Code:
dim FileExistsbol as boolean
dim stFileName as string
stFileName ="C:\Test File.txt"
stFileName =TRIM(stFileName)
FileExistsbol = Dir(stFileName) <> vbNullString
if FileExistsbol then Kill stFileName
 
Last edited:
Cheers for that. Saved me fiddling around.

Here's the same code as a function:
Code:
Public Function FileExists(stFileName As String) As Boolean
Dim FileExistsbol As Boolean
    stFileName = Trim(stFileName)
    FileExistsbol = Dir(stFileName) <> vbNullString
    FileExists = FileExistsbol
End Function
 
If file exists

I came to this post while searching for code similar to the one Ripley mentioned. I save yearly data to a separate file through a function. This is done on first day of january. The file of previous year is saved as 'MyDB2006' (the code adds previous year to the file (Year(Date) -1).

I need a code which will check if 'MyDB2006' exists. I tried the following code

If Dir(D:\YearlyData\MyDB & Year(Date) -1) = 0 Then
'function to create file
Else
MsgBox "File exists"
End If

This is not working. Would anyone please help?
 

Users who are viewing this thread

Back
Top Bottom