Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Detecting existence of a directory

Posted on 2000-04-03
9
Medium Priority
?
216 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: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
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: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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

971 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