Verify if filename is valid

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
 
R

Rajan Lengde

dim fso
set fso = createobject("scripting.flesystemobject")
boolExist = fso.fileexists("C:/Temp/abc.xls")
or boolfolderExists = fso.FolderExists("C:\Test")
set fso = nothing
 
B

Bob Phillips

CBool(Len(Dir(myFileName))) should work fine

--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)
 
D

Dave Peterson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top