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
Else
DExists = 0
End If
End Function
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??
Reply:
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
next
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
next
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
Next
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.
Ted
Reply:
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!
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.