Solved

Creating a directory

Posted on 2011-03-10
9
459 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
[X]
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
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 75

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
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!

 
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
 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

752 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