Connecting Tech Pros Worldwide Help | Site Map

How to determine if directory exists

TTD
Guest
 
Posts: n/a
#1: Nov 13 '05
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.


Rog
Guest
 
Posts: n/a
#2: Nov 13 '05

re: How to determine if directory exists


if len(dir(path)) = 0 then
'create the directory
end if

Allen Browne
Guest
 
Posts: n/a
#3: Nov 13 '05

re: How to determine if directory exists


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" <roger_delahaye@hotmail.com> wrote in message
news:1119276320.468308.100400@f14g2000cwb.googlegr oups.com...[color=blue]
> if len(dir(path)) = 0 then
> 'create the directory
> end if[/color]


Andy Davis
Guest
 
Posts: n/a
#4: Nov 13 '05

re: How to determine if directory exists


Tried that as well. Sorry!

Dawn
"TTD" <ttd@hotmail.com> wrote in message
news:42b6cb6a$0$932$6c56d894@diablo.nl.easynet.net ...[color=blue]
> 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.
>
>[/color]


Trevor Best
Guest
 
Posts: n/a
#5: Nov 13 '05

re: How to determine if directory exists


Allen Browne wrote:[color=blue]
> 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
>[/color]

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]
Trevor Best
Guest
 
Posts: n/a
#6: Nov 13 '05

re: How to determine if directory exists


Trevor Best wrote:[color=blue]
> Allen Browne wrote:
>[color=green]
>> 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
>>[/color]
>
> 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'
>[/color]

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]
james.igoe@gmail.com
Guest
 
Posts: n/a
#7: Nov 13 '05

re: How to determine if directory exists


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

Closed Thread