See if Files, Folders and Drives Exist



Read and add posts to the Visual Basic 6.0 Forums

Download some of my programs and OCXs

Read the Tips here

The old style forum is still available here



Advanced Tip

These functions will allow to determine whether files, folders and drives exist at runtime.  (Taken from AllenOCX)

Code to see whether a file exists:

Public Function FExists(OrigFile As String)
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
FExists = fs.fileexists(OrigFile)
End Function
'Returns a boolean - True if the file exists

Code to see whether a folder exists:

Public Function DirExists(OrigFile As String)
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
DirExists = fs.folderexists(OrigFile)
End Function
'Returns a boolean - True if the folder exists

Code to check the state of a drive (returns 0 if the drive does not exist, 1 if the drive exists but contains no media, 2 if the drive exists and contains media.  Hard-drives will always return 2):

Public Function DExists(OrigFile As String)
Dim fs, d
Set fs = CreateObject("Scripting.FileSystemObject")
    If fs.driveexists(OrigFile) = True Then
    Set d = fs.getdrive(OrigFile)
    DExists = 1
        If d.isready = True Then
        DExists = 2
        Exit Function
        End If
    DExists = 0
    End If
End Function

Archived Comments

From: sajeevan
Date: Sunday, September 5, 2010 at 17:24:00
Comments: This is a very useful site. I got many useful codes form here. Thank u very much.
Iam from Kerala, India. iam a hardware Engineer.
From: Sylvester Marshall
Date: Thursday, September 2, 2010 at 12:17:25
Comments: Thanks to all you wonderful people out there. You wouldn't imagine how easy you make our life so easy and fun to work with.

God Bless you all and pls. keep up the good and self-less service.

From: Taras, Russia
Date: Tuesday, July 27, 2010 at 14:10:27
Comments: You helped me a lot!
Thank you !
From: nishad
Date: Tuesday, October 20, 2009 at 13:38:37
Comments: that was very usefull. thanks alot..
From: Martijn Buning
Date: Wednesday, July 1, 2009 at 16:35:00
Comments: Thanks a bundle, just what i was looking for!
From: shwan
Date: Wednesday, February 4, 2009 at 10:52:24
Comments: Thanks this works great!
Appreciate little snippets like this, I'll use it forever! =) Thanks again
From: Pat
Date: Monday, September 8, 2008 at 17:02:00
Comments: These are very useful information
From: S. Thomas
Date: Sunday, May 4, 2008 at 12:03:51
Comments: Was very useful. Thanks.
From: Robert
Date: Tuesday, April 15, 2008 at 01:50:49
Comments: Thanks for the code, needed it for a MSN bot i am working on.
From: Joe G
Date: Sunday, May 21, 2006 at 03:50:16
Comments: I ran a speed test and the "FileSystemObject" example is about twice as fast as the "If Dir('C:\folder', vbDirectory) = vbNullString" example.

If speed is important use the FileSystemObject!

Joe G.
From: Hasan
Date: Thursday, February 2, 2006 at 17:02:47
Comments: Hello,
i just checked your DRIVE EXIST code...
ok now in my vb project how would i call it?

please let me know.



You need to copy the whole function into your Visual Basic code and then you can call it from any other procedure e.g.:

Private Sub Command1_Click()
If DExists( "a") = 1 Then MsgBox "The A: drive is not yet ready", vbExclamation, "Check Floppy Drive"
End Sub
From: Vinu Lakshman
Date: Monday, July 11, 2005 at 10:37:41
Comments: Hello Martin,
Thanks Martin.
Ure site was really helpful to me.
From: Kristlyn
Date: Monday, May 30, 2005 at 05:37:45
Comments: Tnx for this site... It really helped me a lot in doing my work...=) thanks!!!
From: yurivish
Date: Tuesday, August 10, 2004 at 23:15:57
Comments: cant you just do if dir(filename) <> "" then it exists??

You can do it this way.  It is best to include the attributes for hidden and system files as well so that they can also be detected e.g.:

a = Dir("C:\pagefile.sys", vbHidden + vbSystem)
From: DaMaster
Date: Thursday, August 5, 2004 at 20:25:27
Comments: jam graty:
Actually you can do a code for this instead where you use statements as:

for each folder in root.subfolders

set root as a fso (FileSystemObject) with the folder you want to start searching from. then use a loop-statement to continue searching in subfolders of the subfolders.

in each folder you use following statement:

for each file in folder.files

where you dim file, set folder as a FSO with the value of the current folder.

Example: (this example search through a folders subfolders. use a loop-statement to make the code search thorugh upfollowing subfolders)

Set fso = CreateObject("Scripting.FileSystemObject")
Set Root = fso.Getfolder(App.Path)

For Each folder In Root.subfolders

 For Each ffile In folder.Files 'Root2.Files
   If LCase(ffile) = LCase(AWhereto) Then 'FOUND THE ILE
      msgbox "File found!"
      Exit Sub
      End If


Date: Tuesday, August 3, 2004 at 09:29:42
Comments: your web site is so useful and provide an easy ,creative and more practical way
of learning.
so plz keep on
From: ranganath <ranganath_prasad_babu>
Date: Tuesday, June 22, 2004 at 07:34:37
Comments: Dear Sir/Madam


Ur forums are excellent this will help many people
please continue in the same way

all the best

urs truly

From: Ted Belben
Date: Saturday, February 14, 2004 at 21:32:50

Regerding my previous E-Mail related to your code "See of Files, Folders and Drives Exist" and my question related to creating folders, I think I stumbled on the solution as follows:

Public Sub FoldExists(Folder As String)

' This function checks if required folders exists and if not, will create the folder

Dim MyFileSystem

Set MyFileSystem = CreateObject("Scripting.FileSystemObject")

    If MyFileSystem.FolderExists(Folder) = False Then
       MyFileSystem.CreateFolder (Folder)
    End If

End Sub

I used a Sub rather than a function since the requirement was very simple and not used elsewhere in the code. I just call the sub where needed.

I suggest you publish the solution on your web page for others to use.



You can also use this code to see if a folder exists:

If Dir("C:\folder", vbDirectory) = vbNullString Then 'folder doesn't exist

And this code to create a folder:

MkDir "C:\WINDOWS\DESKTOP\A new folder"
From: Humulus
Date: Friday, October 17, 2003 at 11:18:05
Comments: I had to check if a drive exists from within a Microsoft Access-database. First hit on Google was this. It works great. Thanks!
From: Timothy
Date: Friday, August 1, 2003 at 22:33:24
Comments: Spectacular Code!  Thanks!
From: ManojRajan
Date: Thursday, June 26, 2003 at 13:41:52
Comments: Nice One that really worksssssssss.
Thanks Martin for the help
Actually i was in the middle of a module when i required this stuff. Thanks once again.
From: jam graty
Date: Thursday, November 21, 2002 at 23:30:07
Comments: please help, i need my program to find a file but in your example i don't know were to put it!
Reply: These functions cannot be used to find files, they simply return True or False if a file or folder exists or in the case of the third function, 0, 1 or 2 depending on the state of a specified drive.

You need to use the File Search OCX file to do this - click here.

Martin Allen 1999 - 2011.  Last updated Monday 08 August 2011 07:04:17 PM +0100.