Avatar of GlobaLevel
GlobaLevel
Flag 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...
VB ScriptVisual Basic Classic

Avatar of undefined
Last Comment
GrahamSkan

8/22/2022 - Mon
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
GrahamSkan

Or

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

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
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
GlobaLevel

ASKER
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
GrahamSkan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.