Solved

Creating a directory

Posted on 2011-03-10
9
455 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TT Auto Dashboard 13 92
How to read File Date Created using VB6 8 39
Formula problem with Excel attachment 6 27
VBA - If Bookmark = "XXBOOKMARKXX" then 15 28
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

778 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