Connecting Tech Pros Worldwide Help | Site Map

How to determine if directory exists

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 13th, 2005, 11:11 AM
TTD
Guest
 
Posts: n/a
Default How to determine if directory exists

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.



  #2  
Old November 13th, 2005, 11:11 AM
Rog
Guest
 
Posts: n/a
Default Re: How to determine if directory exists

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

  #3  
Old November 13th, 2005, 11:11 AM
Allen Browne
Guest
 
Posts: n/a
Default 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]


  #4  
Old November 13th, 2005, 11:13 AM
Andy Davis
Guest
 
Posts: n/a
Default 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]


  #5  
Old November 13th, 2005, 11:13 AM
Trevor Best
Guest
 
Posts: n/a
Default 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]
  #6  
Old November 13th, 2005, 11:13 AM
Trevor Best
Guest
 
Posts: n/a
Default 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]
  #7  
Old November 13th, 2005, 11:14 AM
james.igoe@gmail.com
Guest
 
Posts: n/a
Default 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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.