PC Review


Reply
Thread Tools Rate Thread

How determine if directory exists

 
 
windsurferLA
Guest
Posts: n/a
 
      2nd Nov 2007
I know this question has been asked before, but post has expired.

I want to find out if a certain directory exists, and if not to make it.
I know you can use if Dir$(file.xls)<>"" to detect files.
I know I can use MkDir to make the directory if it doesn't exist.

My problem is I don't want to trigger error with MkDir if the directory
already exists.

How do I test to determine if a specific directory exists?
 
Reply With Quote
 
 
 
 
Tim Williams
Guest
Posts: n/a
 
      2nd Nov 2007
Dir() takes a second argument:

If Dir("C:\Stuff", vbDirectory) = "" then
'create folder
End If

Tim

"windsurferLA" <(E-Mail Removed)> wrote in message
news:472ac064$0$24307$(E-Mail Removed)...
>I know this question has been asked before, but post has expired.
>
> I want to find out if a certain directory exists, and if not to make it.
> I know you can use if Dir$(file.xls)<>"" to detect files.
> I know I can use MkDir to make the directory if it doesn't exist.
>
> My problem is I don't want to trigger error with MkDir if the directory
> already exists.
>
> How do I test to determine if a specific directory exists?



 
Reply With Quote
 
RB Smissaert
Guest
Posts: n/a
 
      2nd Nov 2007
This function should do the job:

Function FolderExists(PathName As String) As Boolean
On Error Resume Next
If Len(PathName) > 0 Then
FolderExists = ((GetAttr(PathName) And vbDirectory) > 0)
Err.Clear
End If
End Function


RBS



"windsurferLA" <(E-Mail Removed)> wrote in message
news:472ac064$0$24307$(E-Mail Removed)...
>I know this question has been asked before, but post has expired.
>
> I want to find out if a certain directory exists, and if not to make it.
> I know you can use if Dir$(file.xls)<>"" to detect files.
> I know I can use MkDir to make the directory if it doesn't exist.
>
> My problem is I don't want to trigger error with MkDir if the directory
> already exists.
>
> How do I test to determine if a specific directory exists?


 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      2nd Nov 2007
You can use a function like

Function DirExists(PathName As String) As Boolean
If Dir(PathName, vbDirectory) = vbNullString Then
DirExists = False
Else
DirExists = True
End If
End Function

However, with this code if you pass in a filename (e.g., "C:\Test\Temp.txt")
the function will return True. If you want to ensure that PathName is really
a directory and not a file, use

Function DirExists(PathName As String) As Boolean
If Dir(PathName, vbDirectory) = vbNullString Then
DirExists = False
Else
If Dir(PathName, vbNormal) = vbNullString Then
DirExists = True
Else
DirExists = False
End If
End If
End Function

This will return True only if PathName is the name of an existing directory.
If PathName is a non-existent directory or the name of an existing file, it
will return False.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)



"windsurferLA" <(E-Mail Removed)> wrote in message
news:472ac064$0$24307$(E-Mail Removed)...
>I know this question has been asked before, but post has expired.
>
> I want to find out if a certain directory exists, and if not to make it.
> I know you can use if Dir$(file.xls)<>"" to detect files.
> I know I can use MkDir to make the directory if it doesn't exist.
>
> My problem is I don't want to trigger error with MkDir if the directory
> already exists.
>
> How do I test to determine if a specific directory exists?


 
Reply With Quote
 
windsurferLA
Guest
Posts: n/a
 
      3rd Nov 2007
Thanks for prompt reply... I expect to use your suggestion in
combination with Chip Peason's expansion to preclude the possibility of
a file location being interpreted as a directory.

WindsurferLA


Tim Williams wrote:
> Dir() takes a second argument:
>
> If Dir("C:\Stuff", vbDirectory) = "" then
> 'create folder
> End If
>
> Tim
>
> "windsurferLA" <(E-Mail Removed)> wrote in message
> news:472ac064$0$24307$(E-Mail Removed)...
>> I know this question has been asked before, but post has expired.
>>
>> I want to find out if a certain directory exists, and if not to make it.
>> I know you can use if Dir$(file.xls)<>"" to detect files.
>> I know I can use MkDir to make the directory if it doesn't exist.
>>
>> My problem is I don't want to trigger error with MkDir if the directory
>> already exists.
>>
>> How do I test to determine if a specific directory exists?

>
>

 
Reply With Quote
 
windsurferLA
Guest
Posts: n/a
 
      3rd Nov 2007
Thanks for prompt reply. Your approach would work, but I expect to use
that suggested by Chip Pearson because it precludes inadvertently
thinking that the path name to a file is a directory.

RB Smissaert wrote:
> This function should do the job:
>
> Function FolderExists(PathName As String) As Boolean
> On Error Resume Next
> If Len(PathName) > 0 Then
> FolderExists = ((GetAttr(PathName) And vbDirectory) > 0)
> Err.Clear
> End If
> End Function
>
>
> RBS
>
>
>
> "windsurferLA" <(E-Mail Removed)> wrote in message
> news:472ac064$0$24307$(E-Mail Removed)...
>> I know this question has been asked before, but post has expired.
>>
>> I want to find out if a certain directory exists, and if not to make it.
>> I know you can use if Dir$(file.xls)<>"" to detect files.
>> I know I can use MkDir to make the directory if it doesn't exist.
>>
>> My problem is I don't want to trigger error with MkDir if the
>> directory already exists.
>>
>> How do I test to determine if a specific directory exists?

>

 
Reply With Quote
 
windsurferLA
Guest
Posts: n/a
 
      3rd Nov 2007
Thank you for your prompt reply and fine solution to my problem.
Furthermore, your reply prompted me to look up and learn about
vbNullString, vbDirectory, and vbNormal attributes that I had not
previously encountered or used.

WindsurferLA

Chip Pearson wrote:
> You can use a function like
>
> Function DirExists(PathName As String) As Boolean
> If Dir(PathName, vbDirectory) = vbNullString Then
> DirExists = False
> Else
> DirExists = True
> End If
> End Function
>
> However, with this code if you pass in a filename (e.g.,
> "C:\Test\Temp.txt") the function will return True. If you want to ensure
> that PathName is really a directory and not a file, use
>
> Function DirExists(PathName As String) As Boolean
> If Dir(PathName, vbDirectory) = vbNullString Then
> DirExists = False
> Else
> If Dir(PathName, vbNormal) = vbNullString Then
> DirExists = True
> Else
> DirExists = False
> End If
> End If
> End Function
>
> This will return True only if PathName is the name of an existing
> directory. If PathName is a non-existent directory or the name of an
> existing file, it will return False.
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need function to determine whether a directory exists Bob Valentine Microsoft Access VBA Modules 2 30th Jun 2007 06:39 PM
VBA:: determine if UDF exists? George Microsoft Excel Misc 1 7th May 2007 12:57 PM
How to determine if a Folder/Directory Exists in Excel VBA robinson.william@gmail.com Microsoft Excel Programming 2 17th Nov 2006 02:38 AM
determine if value exists =?Utf-8?B?Z2VlYmVl?= Microsoft Excel Programming 2 19th Sep 2006 11:03 PM
How to determine if directory exists TTD Microsoft Access VBA Modules 2 21st Jun 2005 01:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:49 PM.