Solved

Detecting existence of a directory

Posted on 2000-04-03
9
209 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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 32

Expert Comment

by:bhess1
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 50 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
Technology Partners: 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:bhess1
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:bhess1
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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

679 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