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
Solved

Detecting existence of a directory

Posted on 2000-04-03
9
206 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

790 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