Link to home
Start Free TrialLog in
Avatar of GlobaLevel
GlobaLevelFlag for United States of America

asked on

vba check to seeif a path is valid

I need to check to see if a path is valid....I want to verify that"C:/program Files/superprogram/setup.exe "  is a valid program  and return a result a message should thatnot a valid path...
Avatar of rettiseert
rettiseert

Hui, try this:

dim filesys
Set filesys = CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("c:\somefile.txt") Then
   msgbox "The path is valid"
else
   msgbox "The path is NOT valid"
end if
Avatar of GrahamSkan
Or

If Dir$("C:\MyFolder", vbDirectory) = "" Then
   MsgBox "No such folder"
Else
    MsgBox "The folder exists"
End If
Bowing to GrahamSkan's superior knowledge, I'd like to approve his method, but humbly offer one tiny improvement. Change:
Dir$("C:\MyFolder", vbDirectory) = ""
to
Len(Dir$("C:\MyFolder", vbDirectory) )
 My boss years ago did a very comprehensive test to see what VB statements were the fastest. The Len statement was much faster than string comparison, even to an empty string. Just a sidebar, he also found that the Instr statement was among the fastest, and the Val(text) statement was among the slowest.
Avatar of GlobaLevel

ASKER

I need the vba solution, not vbscript...

this vbscript correct?

dim filesys
Set filesys = CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("c:\somefile.txt") Then
   msgbox "The path is valid"
else
   msgbox "The path is NOT valid"
end if

-=-=-=-

I've tried this, with the length of the program in a loop, sometimes it works, sometimes it doesnt...


-=-=-=

vbaclassicguy,

I will jave to try this tmrw...



Len(Dir$("C:\MyFolder", vbDirectory) )


If Dir$("C:\MyFolder", vbDirectory) = "" Then
   MsgBox "No such folder"
Else
    MsgBox "The folder exists"
End If
mistyped...

I've tried this, with the length of the program in a loop, sometimes it works, sometimes it doesnt...

If Dir$("C:\MyFolder", vbDirectory) = "" Then
   MsgBox "No such folder"
Else
    MsgBox "The folder exists"
End If

-=-=-=

vbaclassicguy,

I will jave to try this tmrw...

Len(Dir$("C:\MyFolder", vbDirectory) )
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial