Connecting Tech Pros Worldwide Help | Site Map

How to determine if directory exists

  #1  
Old November 13th, 2005, 12:11 PM
TTD
Guest
 
Posts: n/a
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, 12:11 PM
Rog
Guest
 
Posts: n/a

re: How to determine if directory exists


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

  #3  
Old November 13th, 2005, 12:11 PM
Allen Browne
Guest
 
Posts: n/a

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, 12:13 PM
Andy Davis
Guest
 
Posts: n/a

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, 12:13 PM
Trevor Best
Guest
 
Posts: n/a

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, 12:13 PM
Trevor Best
Guest
 
Posts: n/a

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, 12:14 PM
james.igoe@gmail.com
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
WebRequest to see if Directory exists Steven J. Reed answers 1 November 23rd, 2005 05:17 AM
Problem trying to determine if a file exists using wildcard Dan W answers 3 November 21st, 2005 09:21 PM
System.IO, how to determine if valid path format WALDO answers 1 November 21st, 2005 07:04 AM
How to determine if an instance of your program is already running? Mike answers 5 July 18th, 2005 08:07 AM