?
Solved

Detecting existence of a directory

Posted on 2000-04-03
9
Medium Priority
?
215 Views
Last Modified: 2010-05-02
I want to check a specified drive for the existence of a directory.
If the directory doesn't currently exist I will then create it. If it does exist I will not worry about creating it.

How do I go about checking for the presence of a directory?
0
Comment
Question by:mallards
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 2681449
This code can be used.

Dim S$
S = Dir("C:\TEMP\.",vbDirectory) ' change to match your directory.  The period is important

If S = "." Then
   ' It's a directory, and it exists!
Else
   S=Dir("C:\TEMP")
   If S <> "" Then
      ' some file of that name exists!
   Else
      ' MkDir "C:\TEMP"
   End If
End If
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 200 total points
ID: 2681464
Dim sDir As String
sDir = "c:\test"
If Dir(sDir, vbDirectory) = "" Then
    MkDir sDir
    Debug.Print sDir & " created"
Else
    Debug.Print sDir & " already exists"
End If
0
 

Expert Comment

by:Jagertee
ID: 2684407
Function ExistPath(MyPath) As Boolean
  On Error Resume Next

  Dim x as String

  'save current directory
  x = CurDir

  'try to change to MyPath
  ChDir MyPath
  If Err=0 Then
    'yes, it's there!
    ExistPath = True
  End If

  'back to current directory
  ChDir x

  Err = 0
End Function
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mallards
ID: 2685598
Quicker and more efficient way to do it. see Erick37
0
 

Author Comment

by:mallards
ID: 2685602
Great! What efficient code. Thanks alot!
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2685615
Thanks. Glad it helped!
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 2690218
Just a small warning - if a file exists with the same name as your proposed directory, then the test will return 'Directory Exists' (as of VB5 - Is this fixed in VB6?)
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2690378
I think it's an operating system issue.  In Win95, you cannot have a file with the same name of a directory in the same level.
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 2690538
I know that - what I was noting was that, even though you specify vbDirectory on the DIR call, it will still return any files AND directories that match your spec, as if vbDirectory had not been specified.  This is a bug.

So, if I wanted to find out if the directory C:\Autoexec.BAT existed, the call would happily return that there was a directory of that name.  Try to ChDir to there, and... Error!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question