Solved

Detecting existence of a directory

Posted on 2000-04-03
9
196 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
 

Author Comment

by:mallards
ID: 2685598
Quicker and more efficient way to do it. see Erick37
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now