Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Exists Using FSO

Status
Not open for further replies.

PrgrmsAll

Programmer
Apr 8, 2003
180
US
I want to use the File Scripting Object to check whether or not an Access Database (*.mdb) with a known name exists in a directory location. What references do I need to add and what is the syntax for checking the existence of a file using this object.

Thank you in advance.
 
If you want to use FSO then add a reference to the Microsoft Scripting Runtime and try the following:

Code:
    Dim objFileScripting, objFolder
    Dim FileName, filecollection
    Dim strFilename As String
    
    strFilename = "test.doc"
    Set objFileScripting = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFileScripting.GetFolder("C:\temp")
    Set filecollection = objFolder.Files
    For Each FileName In filecollection
        If FileName.Name = strFilename Then MsgBox "File Found"
    Next

If the file is found you will get a message box informing you of this. You may however want to have a look at the Dir command as this might be a lot simpler for what you need.
 
In order to use the FSO you must include MICROSOFT SCRIPTING RUNTIME as a reference or (Scrrun.dll) than to declare a file system object you do:

dim fso as new scripting.filesystemobject

im not sure but i think you may also be able to simply just do this

dim fso as new filysystemobject

you can use the getfile function and than just search for an error.

set fil=fso.getfile("c:\nameofyourfile.txt")

if that produces and error...file doesnt exist. I believe that should work. I know there is a built in function filesystemobject.folderexists that will see if the folder exists. But that should work
 
Goodness, we're going around the houses a bit with the FSO solution...
[tt]
Public Function vbFileExists(strPath As String) As Boolean
With New FileSystemObject
vbFileExists = .FileExists(strPath)
End With
End Function
 
Thanks all of you. Looks like the DIR funtion will work admirably. If I found the file and wanted to delete it, would the FSO be the way to go?
 
{b]strongm[/b] - That was a bit long winded wasn't it! I forgot about FileExists!

{b]chrishunter21[/b] - To delete the file you could use the Kill command.
 
sure, you could just use the file.delete or filesystemobject.deletefile

and thanks strongm...completely forgot about the filesystemobject.fileexists
 
Problem solved. Thanks everyone, you've all been very helpful indeed.
 
I know you asked about FSO, but consider this alternative:
[tt]
If Len(Dir("c:\fred.txt")) > 0 Then Kill("c:\fred.txt")
[/tt]
That's it - all done!


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
johnwm - thank you, sir. that is excellent. i am all about the brevity in code and this does the trick perfectly. thanks to everyone else because now i have a handle on the FSO, too. brilliant!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top