469,323 Members | 1,665 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

How to determine if directory exists

TTD
Hello,

I need to create a directory in code, but when de directory already exists,
I get err.number 75.

Since the creation already is in the part where a former error is sent, I
can't catch this error.

In VB there is this code
path = "C:\temp\"
If Directory.Exists(path) = False Then
' Create the directory.
Directory.CreateDirectory(path)
End If

Is there a way to determine if C:\temp already exists before I create one?

Thanks for your time.
Nov 13 '05 #1
6 13969
Rog
if len(dir(path)) = 0 then
'create the directory
end if

Nov 13 '05 #2
Might be good to use the vbDirectory argument of Dir():

Public Function FolderExists(varPath As Variant) As Boolean
On Error Resume Next
If Len(varPath) > 0 Then
FolderExists = (Len(Dir$(varPath, vbDirectory)) > 0&)
End If
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Rog" <ro************@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
if len(dir(path)) = 0 then
'create the directory
end if

Nov 13 '05 #3
Tried that as well. Sorry!

Dawn
"TTD" <tt*@hotmail.com> wrote in message
news:42*********************@diablo.nl.easynet.net ...
Hello,

I need to create a directory in code, but when de directory already
exists,
I get err.number 75.

Since the creation already is in the part where a former error is sent, I
can't catch this error.

In VB there is this code
path = "C:\temp\"
If Directory.Exists(path) = False Then
' Create the directory.
Directory.CreateDirectory(path)
End If

Is there a way to determine if C:\temp already exists before I create one?

Thanks for your time.

Nov 13 '05 #4
Allen Browne wrote:
Might be good to use the vbDirectory argument of Dir():

Public Function FolderExists(varPath As Variant) As Boolean
On Error Resume Next
If Len(varPath) > 0 Then
FolderExists = (Len(Dir$(varPath, vbDirectory)) > 0&)
End If
End Function


Another method I've used in the past (Before BASIC could find
direcrtories) is to append \nul [1] to the path as the nul device exists
in any valid path.

[1] Note in an OS nul is spelt with a single l, not "null" as in Access'

--
[OO=00=OO]
Nov 13 '05 #5
Trevor Best wrote:
Allen Browne wrote:
Might be good to use the vbDirectory argument of Dir():

Public Function FolderExists(varPath As Variant) As Boolean
On Error Resume Next
If Len(varPath) > 0 Then
FolderExists = (Len(Dir$(varPath, vbDirectory)) > 0&)
End If
End Function


Another method I've used in the past (Before BASIC could find
direcrtories) is to append \nul [1] to the path as the nul device exists
in any valid path.

[1] Note in an OS nul is spelt with a single l, not "null" as in Access'


Actually, this may be the safer option, as vbDirectory will find files
as well as directories, you could very well find a file that you thought
was a directory.

--
[OO=00=OO]
Nov 13 '05 #6
dim bExists as boolean

bExists = FileExistsDir("path")

if bExist = False
'create directory
end if
Function FileExistsDIR(sFile As String) As Boolean
FileExistsDIR = True
If Dir$(sFile) = vbNullString Then FileExistsDIR = False
End Function

Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Pgar | last post: by
1 post views Thread by Steven J. Reed | last post: by
1 post views Thread by Phil Barber | last post: by
5 posts views Thread by Santel | last post: by
9 posts views Thread by shughes | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.