Solved

Creating a directory

Posted on 2011-03-10
9
451 Views
Last Modified: 2012-05-11
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.

0
Comment
Question by:mlmcc
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35100100
Try this:
If Dir("c:\some\dir") <> "" Then
    ' create directory
Else

Open in new window

0
 
LVL 5

Expert Comment

by:MedievalWarrior
ID: 35100468
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

0
 
LVL 14

Accepted Solution

by:
VBClassicGuy earned 125 total points
ID: 35100616
     If Len(Dir(YourDir, 16)) Then
         If (GetAttr(YourDir) And 16) = 16 Then
            'directory exists
         End If
      End If
0
 
LVL 100

Author Comment

by:mlmcc
ID: 35103119
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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Assisted Solution

by:MedievalWarrior
MedievalWarrior earned 125 total points
ID: 35103396
Yes. It was a good idea by VBClassicGuy but it also depends, if you don't know before hand if it's a directory and the path in question would point to a file then the additional check might be useful although it depends on if you already are pulling a list of directories and know if there wouldn't be files in the path.

Here is another one based on VBClassicGuy example that uses Path functions.
Option Explicit
 
Private Declare Function PathFileExistsW Lib "shlwapi.dll" (ByVal pszPath As Long) As Long
Private Declare Function PathIsDirectoryW Lib "shlwapi.dll" (ByVal pszPath As Long) As Long

Private Function DirectoryExists(ByVal szPath As String) As Boolean
  If PathIsDirectoryW(StrPtr(szPath)) Then
    DirectoryExists = PathFileExistsW(StrPtr(szPath))
  Else
    DirectoryExists = False
  End If
End Function

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

Open in new window

0
 
LVL 100

Author Comment

by:mlmcc
ID: 35103754
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
0
 
LVL 5

Expert Comment

by:BrianVSoft
ID: 35107074
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..
0
 
LVL 14

Expert Comment

by:VBClassicGuy
ID: 35108681
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).
0
 
LVL 100

Author Comment

by:mlmcc
ID: 35117875
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
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

707 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