Check to see if a file exists

DocNice

Registered User.
Local time
Today, 00:19
Joined
Oct 6, 2004
Messages
76
I need to do a simple check to see if a file exists.

Ie -
If C:Myfile.doc exists Then
a
else
b
end if

The reason I need this is I'm creating an XML file, but the script I'm using only appends to the end of the file, rather than overwriting it.

FYI, the script is:
Set fl = New Scripting.FileSystemObject
Set txt = fl.OpenTextFile(varCurrentDirectory & "\" & filename1 & "_Attachment1.xml", ForAppending, True)
 
Code:
If Dir("C:\YourFile.xml") = "" then
     msgbox "file does not exist"
Else
     msgbox "file does exist"
End If
Search the Access help files or this forum for more info on how to use the Dir() function for files and directories.
 
Also...

Code:
Set fl = New Scripting.FileSystemObject
MsgBox fl.FileExists("C:Myfile.doc")
 
Doc..

there's a handy tool on this site that begins with "S" and ends with "earch". I know that hudson and myself have posted on the filesystemobject with regards to checking/modifying a file.

Please don't discredit the help we've given in the past. Use the search ability - it's there for a reason.
 
modest,
Sorry, I didn't realize you were the hall monitor. Who says I didn't do a search? I've been on here 6 months, programming my database every day, and have asked only about a dozen questions. Perhaps I didn't find anything this time because I didn't come out of the womb knowing that finding and replacing files involves filesystemobject. I gotta pay more attention in kindergarten.

ghudson and lagbolt,
Thanks for your help, I really appreciate your time. I'll do a search on the Dir function.
Thanks!
Doc
 
The filesystemobject does some cool stuff with files but there are VBA ways of doing things also. If you just want to delete a file if it exists then try this...

Code:
If Dir("C:\YourFile.xml") <> "" then
     msgbox "file does exist" 
     Kill("C:\YourFile.xml")
     'do your stuff here
Else
     msgbox "file does not exist"
End If
 
DocNice said:
Who says I didn't do a search?

Doc, I know you have posted here, which is why I didn't care to be so hasty with you :p But I mean, what were you searching for? There are plenty of returns on File Exists.

And as a general note to anyone needing to modify a file, the FileSystemObject is an important object to look at... after all, it's name is self explanatory "File System" meaning almost anything dealing with file/storage information.

Cheers, Modest
 
Modest, your a clown. Every time I read a post with your input it involves some animosity.

Meboz
 
meboz said:
Modest, your a clown. Every time I read a post with your input it involves some animosity.

Meboz

Modest needs to change his name to "Getting Annoyed" because the "not so intelligent" people of the forum are starting to upset him and make him talk in third person.

Seriously though, my stress ball is all stressed out.
 

Users who are viewing this thread

Back
Top Bottom