How to detect if a directory already exists using "If" statement
How do i detect if a folder(directory) already exists using the if statement?
PanasonicSubz
Re: How to detect if a directory already exists using "If" statement
Re: How to detect if a directory already exists using "If" statement
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
Re: How to detect if a directory already exists using "If" statement
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
Re: How to detect if a directory already exists using "If" statement
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