Cannot get Dir() to verify that file exists.

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
833
Office Version
  1. 365
Platform
  1. Windows
Am merely trying to see if a file exists. Using Dir(). I confirmed that the path exists using, Dir(sPath, vbDirectory). When wrapping the Dir() in a function to look for the file I use

VBA Code:
If Dir(psPath, vbNormal) <> "" Then PathExists = True

and get a Run-time error 52 and message that "Bad file name or number".

If I use Dir() directly in my sub there is no error message but Dir does not find my file.

VBA Code:
    If Dir(sPath & sFileName_Week1, vbNormal) = "" _
     Then bFileExistsWeek1 = False

VBA Code:
   If Dir(sPath & sFileName_Week1) = "" _
     Then bFileExistsWeek1 = False

So I know that the path exists and that it ends with a "\". I verified that the file name is correct and that it is in the path that was confirmed to exist.

I tried this code which works (and which will serve my need).

If Dir(sPath & sFileName_Week1) <> "" _
Then bFileExistsWeek1 = True

So curiosity got to me. What am I missing?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Could you provide what is populated in these variables:
sPath
sFileName_Week1
 
Upvote 0
Like I said, path and file do exist!

path: C:\Users\Jim\Desktop\GLK\Tip Log\
file name: VaultLog_22-04-18.xlsm
 
Upvote 0
Hmm, this is tough to determine, and is hard to test without trouble shooting on your end. I'm not getting any errors when I run this code:
VBA Code:
Sub TestDIR()

Dim spath As String
Dim sFile As String

spath = "U:\Test\Test1\"
sFile = "TestA.xlsx"

If Dir(spath & sFile, vbNormal) <> "" Then MsgBox True

End Sub

To confirm pspath is the same as spath? If so, perhaps try this:
VBA Code:
If Dir(psPath & "*.xl*", vbNormal) <> "" Then PathExists = True

The only thing I can think of is there's something in that folder that is causing DIR to not work as intended.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,692
Members
449,117
Latest member
Aaagu

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