Solved

Access 2002 - Create a new directory

Posted on 2008-06-18
19
303 Views
Last Modified: 2013-11-28
hello!

I want to accomplish two things:
1)  To check and see if a directory exists or not
2)  To create a new directory off any drive letter.  Example:  C:\NewDirectoryt

Is there a procedure in VB that already does this ?

Thank you!
0
Comment
Question by:ellenjbr
  • 8
  • 6
  • 5
19 Comments
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 500 total points
ID: 21819175
Use this snippet:


   
    If Len(Dir("C\SomePath\SomeMorePath", vbDirectory)) = 0 Then
        MkDir "C\SomePath\SomeMorePath"
    End If
   
0
 

Author Closing Comment

by:ellenjbr
ID: 31468662
Great!  Thanks!
0
 
LVL 75
ID: 21819387
You are welcome ...

mx
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 21819557
ellenjbr,

I have been using this for a while:

On Error Resume Next
Dim strFolderName As String
Dim varArray As Variant '(Array)
Dim strFinalName As String
Dim intCounter As Integer
strFolderName = InputBox("Enter Folder Name:" _
                & vbCrLf & "(ex: C:\YourNewFolderName)")
varArray = Split(strFolderName, "\")
strFinalName = varArray(0)
    For intCounter = 1 To UBound(varArray)
        strFinalName = strFinalName & "\" & varArray(intCounter)
        MkDir strFinalName
    Next intCounter


Modified from this link:
http://www.tek-tips.com/viewthread.cfm?qid=1161048&page=1

You can obviously convert this to a function, if need be.

JeffCoachman

0
 
LVL 75
ID: 21819565
?
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 21819850
??
0
 

Author Comment

by:ellenjbr
ID: 21819953
boag2000 - Hi and thank you for your input.  I only want to create the one folder.   Also, on testing the previous code, it errored after the folder was created... evidently it does not recognize when it checks
I wrote this:

dim valDir as string

valDir = "C:\newfolder"
if Len(dir(valDir)) = 0 then
  MkDir valDir
endif

Is there no CheckDir or IsDir function/procedure in VB?  Thanks!

0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 21821874
ellenjbr,

You actually accepted that post shortly before I posted mine.
So I presumed it was working for you.
I don't know of any "CheckDir" command for DOS.

(I have seen "If Exists" used in some instances though.)

I moded my code like so, to alert me (With a popup messagebox) if the folder/Directory already exists:

On Error GoTo Err_MakeDirFolder
Dim strFolderName As String
Dim varArray As Variant '(Array)
Dim strFinalName As String
Dim intCounter As Integer
strFolderName = InputBox("Enter Folder Name:" _
                & vbCrLf & "(ex: C:\YourNewFolderName)")
varArray = Split(strFolderName, "\")
strFinalName = varArray(0)
    For intCounter = 1 To UBound(varArray)
        strFinalName = strFinalName & "\" & varArray(intCounter)
        MkDir strFinalName
    Next intCounter

Exit_MakeDirFolder:
    Exit Sub

Err_MakeDirFolder:
    'If the Folder/Directory already exists
    If Err.Number = 75 Then
        MsgBox "The folder: " & "'" & strFinalName & "'" & " already exists."
        Resume Exit_MakeDirFolder
    Else
        MsgBox "There was an error executing the command." _
        & vbCrLf & "Error " & Err.Number & ": " _
        & vbCrLf & Error, vbExclamation
        Resume Exit_MakeDirFolder
    End If




JeffCoachman
0
 

Author Comment

by:ellenjbr
ID: 21822360
Jeff:

Hello!  I take that the word 'resume' allows one to escape without getting an Access or Windows error message ...I'll have to try the code later ... I think that the first line of the code mentioned earlier Len(Dir(PRIV)) = 0, apparently doesn't work.

Is there a delete dir command as well ... thank you.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 21822843
ellenjbr,

The 'resume' part is just part of the standard Access error handling code.
It goes back to a theory that says; a sub or function should have one exit point.

You can use:
RmDir "C:\YourFolderName"
... to delete a folder/directory.

JeffCoachman
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 21822870
boag2000,

But I am still confused,
...Did MX's stated code work for you?

JeffCoachman
0
 
LVL 75
ID: 21823096
The code I posted is in daily use - with no errors.

mx
0
 
LVL 75
ID: 21824509
I'm confused ... is there an issue?

mx
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 21826047
MX,

I'm not sure, see post:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_23497685.html#a21819953
< Also, on testing the previous code, it errored after the folder was created>

Let's wait and see.

Jeff
0
 
LVL 75
ID: 21826105
Yes .... not clear ... your code  or my code.  I pulled that snippet right out of my app launcher.  It checks for the existance of the dir, if not there - creates it ... then copies the front end and library file down to the users C:\SomeDir.  Again, in daily use.

mx
0
 

Author Comment

by:ellenjbr
ID: 21828878
hi ...

  If Len(Dir("C\SomePath\SomeMorePath", vbDirectory)) = 0 Then
        MkDir "C\SomePath\SomeMorePath"
    End If

the above code, in tact, does not work .... if I add a : for c:\ , and other variations, it still does not work.  only what I posted earlier works re:  making the directory.  The first line of the code doesn't do what you are wanting.

Thank you for the rmdir .... I will test out the complete code asap and get back with you on it.... thanks!
0
 
LVL 75
ID: 21828903
I see the problem ... I left off back slashes at the end of the path - because I actually have string variables - not hard coded paths.

    If Len(Dir("C\SomePath\SomeMorePath\", vbDirectory)) = 0 Then
        MkDir "C\SomePath\SomeMorePath\"
    End If

The snipped above is from this (part of a function)

    Dim obj As Object
    Dim mVersion As String
    Dim DbName As String
    Dim DbRefLib1 As String
    ''Dim DbRefLib2 As String
    Dim DbImg1 As String
    Dim DbImg2 As String
    Dim DbSrcFldrPgrm As String
    Dim DbSrcFldrLib As String
    Dim DbSrcFldrImg As String
    Dim DbDestFldr As String
     
   
    DbSrcFldrPgrm = "\\xyz\workgroup\CSBU2\BPO-CSBU\Mass AOR Desk\zzFEMasters\"
    DbSrcFldrImg = "\\xyz\workgroup\CSBU2\BPO-CSBU\Mass AOR Desk\zzFEMastersImg\"
    DbSrcFldrLib = "\\xyz\workgroup\CSBU2\BPO-CSBU\Mass AOR Desk\zzLIBMasters\"
   
    DbName = "MOPPgrm.mdb"
    DbImg1 = "MOPPgrm.bmp"
    DbImg2 = "MOPLogo.bmp"
    DbRefLib1 = "ADIvba09LIB.mda"
    DbDestFldr = "C:\MassDatabases\"
       
    If Len(Dir(DbDestFldr, vbDirectory)) = 0 Then
        MkDir DbDestFldr
    End If
   
    FileCopy DbSrcFldrImg & DbImg1, DbDestFldr & DbImg1
    FileCopy DbSrcFldrImg & DbImg2, DbDestFldr & DbImg2
    FileCopy DbSrcFldrLib & DbRefLib1, DbDestFldr & DbRefLib1
    '''FileCopy DbSrcFldrLib & DbRefLib2, DbDestFldr & DbRefLib2

    FileCopy DbSrcFldrPgrm & DbName, DbDestFldr & DbName

This code gets called  many times a day by many users ....

mx
0
 

Author Comment

by:ellenjbr
ID: 21843784
Your code works fine ... thanksl
0
 
LVL 75
ID: 21843793
Yes ... I'm using it in another app as we 'speak'.

mx
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

861 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