Check file or folder exists

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
The following code to check if a file or folder exists is taken from here:


This checks for a file:

Rich (BB code):
Sub Test_File_Exist_With_Dir()

    Dim FilePath As String
    Dim TestStr As String

    FilePath = "C:\Users\Ron\test\book1.xlsm"

    TestStr = ""
    On Error Resume Next
    TestStr = Dir(FilePath)
    On Error GoTo 0
    If TestStr = "" Then
        MsgBox "File doesn't exist"
    Else
        MsgBox "File exist"
    End If

End Sub


whereas this checks for a folder:


Rich (BB code):
Sub Test_Folder_Exist_With_Dir()
    Dim FolderPath As String

    FolderPath = "C:\Users\Ron\test"
    If Right(FolderPath, 1) <> "\" Then
        FolderPath = FolderPath & "\"
    End If
    
    If Dir(FolderPath, vbDirectory) <> vbNullString Then
        MsgBox "Folder exist"
    Else
        MsgBox "Folder doesn't exist"
    End If
End Sub


The problem I'm getting is if I pass a string such as:


Rich (BB code):
"C:\Users\Ron\test"


into the Sub Test_File_Exist_With_Dir, it still returns true, even though I've only passed in a folder location and not the name of a file.


How can I amend this?


Thanks

(although using the FSO method does work).
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I don't think you can really.

When you pass 'C:\Users\Ron\test' to Test_File_Exist_With_Dir how does VBA know that 'test' is a folder and not a file?
 
Upvote 0
I don't think you can really.

When you pass 'C:\Users\Ron\test' to Test_File_Exist_With_Dir how does VBA know that 'test' is a folder and not a file?

I see, yet using the fso method, it does somehow differentiates between them.
 
Upvote 0
How are you checking with FSO?

It has different, specific methods for checking for folders/files.
 
Upvote 0
How are you checking with FSO?

It has different, specific methods for checking for folders/files.

I passed a folder (as opposed to a file) into the argument:

Rich (BB code):
]
Sub Test_File_Exist_FSO_Late_binding() 'No need to set a reference if you use Late binding Dim FSO As Object Dim FilePath As String Set FSO = CreateObject("scripting.filesystemobject") FilePath = "C:\Users\Ron\test\" ' CHANGED IT HERE If FSO.FileExists(FilePath) = False Then MsgBox "file doesn't exist" Else MsgBox "File exist" End If End Sub



 
Upvote 0
The problem I'm getting is if I pass a string such as "C:\Users\Ron\test" into the Sub Test_File_Exist_With_Dir, it still returns true, even though I've only passed in a folder location and not the name of a file.
Remove the vbDirectory attribute.
 
Last edited:
Upvote 0
Code:
 If Dir(FolderPath[COLOR="#FF0000"], vbDirectory[/COLOR]) <> vbNullString Then
 
Upvote 0
This
Code:
"C:\Users\Ron\test"
gives me "File doesn't exist"
but this
Code:
"C:\Users\Ron\test[COLOR=#ff0000]\[/COLOR]"
returns "File exist"
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top