Solved

Creating a directory

Posted on 2011-03-10
9
453 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 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

919 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

21 Experts available now in Live!

Get 1:1 Help Now