Click to See Complete Forum and Search --> : How to detect if a directory already exists using "If" statement


PanasonicSubz
November 28th, 1999, 10:33 PM
How do i detect if a folder(directory) already exists using the if statement?

PanasonicSubz

czimmerman
November 28th, 1999, 10:51 PM
See http://www.freevbcode.com/ShowCode.Asp?ID=501

Aziz Errafay
November 30th, 1999, 05:02 AM
ry an error handler on the chdir command:


Sub ChangePath( sPath as string )
on error GoTo errDir

ChDir (sPath)

Exit Sub

errDir:

If Err.Number = 76 then
MsgBox "Yep, path " + sPath + " doesn't exist"
else
MsgBox Err.Description, vbOKOnly, Err.Number
End If

End Sub

Chris Eastwood
November 30th, 1999, 07:37 AM
Place a reference in your project to Microsoft Scripting Runtime (sscrun.dll) and use the following code :



Dim oFSO as FileSystemObject

set oFSO = new FileSystemObject

If oFSO.FolderExists("c:\temp\whatever") then
'
else
'
End If




It's probably the easiest method available, and you get a whole bunch of other functionality from using the Scripting Objects.



Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb

jusclev
December 1st, 1999, 12:19 AM
Try using the Dir function
Dir[(pathname[, attributes])]


'Variable declaration
Dim pathname as string

'Variable initialization
pathname = "Type/Retrieve directory name"

'Test existence
If (Dir(pathname, vbDirectory) <> "") then
'Directory Exists
'Dir fctn returns directory name
else
'Directory does not exist
'Dir fctn returns empty string
End If