Verify if filename is valid

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I need to say if a specific path refer to a valid file. It tried to use the
function below but it give true if the directory is valid.
ex:
myFileName = "C:\Temp" should answer false (all the time)
myFileName = "C:\Temp\Test.xls" should answer true (if file exist)

I tried that but doesn't work.
Len(Dir$((myFileName,vbdirectory)
Len(myFileName)
CBool(Len(Dir(myFileName)))

Thanks
 
dim fso
set fso = createobject("scripting.flesystemobject")
boolExist = fso.fileexists("C:/Temp/abc.xls")
or boolfolderExists = fso.FolderExists("C:\Test")
set fso = nothing
 
CBool(Len(Dir(myFileName))) should work fine

--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)
 
You could use dir() like this:

Option Explicit
Sub testme()
Dim TestStr As String
Dim myFileName As String

myFileName = "C:\temp"

TestStr = ""
On Error Resume Next
TestStr = Dir(myFileName & "\nul")
On Error GoTo 0

If TestStr = "" Then
On Error Resume Next
TestStr = Dir(myFileName)
On Error GoTo 0
If TestStr = "" Then
MsgBox "Not a folder, not an existing file!"
Else
MsgBox "It's a file!"
End If
Else
MsgBox myFileName & " is a folder!"
End If

End Sub

Nul: is one of those DOS devices that belong with every folder.
 
Back
Top