Link to home
Start Free TrialLog in
Avatar of Mike McCracken
Mike McCracken

asked on

Creating a directory

I know how to create a directory but I need to check if it exists before creating it.  Normally the directory will not exist so there is no issue however if a user needs to rerun reports due to changed data the directory will exist and the creation fails.

Avatar of kaufmed
kaufmed
Flag of United States of America image

Try this:
If Dir("c:\some\dir") <> "" Then
    ' create directory
Else

Open in new window

Here is another example using Path functions
Option Explicit
 
Private Declare Function PathFileExistsW Lib "shlwapi.dll" (ByVal pszPath As Long) As Long

Private Function Exists(ByVal szPath As String) As Boolean
  Exists = PathFileExistsW(StrPtr(szPath))
End Function

Private Sub Form_Load()
  Debug.Print Exists("c:\windows\system32")
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of VBClassicGuy
VBClassicGuy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike McCracken
Mike McCracken

ASKER

I tried
If Dir("c:\some\dir") <> "" Then
    ' create directory
End if

It always is false whether the directory is there or not.  I tried using = and it always wants to create the directory.
That ois basically the method I had tried after findingg it on another site.


VBClassicGuy - WHy the need for 2 tests.  The first test fails if the directory doesn't exist or is that because it might be a file name and hte second test verifies it is a directory?

MedievalWarrior- That works.

mlmcc
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am writing an application that will produce numerous reports for various projects.  The reports for a project need to go into the same directory using the project name as the directory name.  I need to create the directory if it doesn't exist.  It should be in a directory that has no files but that is probably a good check.

mlmcc
You should use the vbDirectory option with the Dir( function
Eg.
  If Dir(MyDirPath, vbDirectory) = "" Then MkDir MyDirPath

You can also just put an "On Error Resume Next" before the MkDir statement
That way, if the Folder is already there, the MkDir does nothing..
Eg.
  On Error Resume Next
   MkDir MyDirPath
  'Re-assign previous "On Error goto" here..
Your second assumption is the correct one, mimcc. Two tests are needed. I've had the first test fail, especially if a file and a folder both exist with the same name. So the two tests make double-damn-sure it is a directory.

By the way, Brian, vbDirectory = 16. I just use the 16 (less typing).
I used the following in a function with the appropriate declares

   If PathIsDirectoryW(StrPtr(szPath)) Then
      DirectoryExists = PathFileExistsW(StrPtr(szPath))
   Else
      DirectoryExists = False
   End If
 
Thanks
mlmcc