These functions will allow to determine whether files, folders
and drives exist at runtime. (Taken from
Code to see whether a file exists:
Function FExists(OrigFile As
Set fs =
FExists = fs.fileexists(OrigFile)
'Returns a boolean - True if the file exists
Code to see whether a folder exists:
Function DirExists(OrigFile As
Set fs = CreateObject("Scripting.FileSystemObject")
DirExists = fs.folderexists(OrigFile)
'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):
Function DExists(OrigFile As
Dim fs, d
Set fs =
= True Then
Set d = fs.getdrive(OrigFile)
DExists = 1
If d.isready = True Then
DExists = 2
DExists = 0
comments powered by Disqus. blog comments powered by
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
Iam from Kerala, India. iam a hardware Engineer.
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 !
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!
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
Date: Monday, September 8, 2008 at 17:02:00
Comments: These are very useful information
Date: Sunday, May 4, 2008 at 12:03:51
Comments: Was very useful. Thanks.
Date: Tuesday, April 15, 2008 at 01:50:49
Comments: Thanks for the code, needed it for a MSN bot i am working on.
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!
Date: Thursday, February 2, 2006 at 17:02:47
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"
Date: Monday, July 11, 2005 at 10:37:41
Comments: Hello Martin,
Ure site was really helpful to me.
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!!!
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)
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
For Each ffile In
If LCase(ffile) =
LCase(AWhereto) Then 'FOUND
msgbox "File found!"
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
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
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
Set MyFileSystem = CreateObject("Scripting.FileSystemObject")
If MyFileSystem.FolderExists(Folder) =
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"
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!
Date: Friday, August 1, 2003 at 22:33:24
Comments: Spectacular Code! Thanks!
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.
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
Martin Allen 1999 - 2011. Last updated
Monday 08 August 2011 07:04:17 PM +0100.