Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating a directory

Posted on 2011-03-10
9
Medium Priority
?
464 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 500 total points
ID: 35100616
     If Len(Dir(YourDir, 16)) Then
         If (GetAttr(YourDir) And 16) = 16 Then
            'directory exists
         End If
      End If
0
Industry Leaders: 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 101

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

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 101

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

688 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