Solved

Access 2002 - Create a new directory

Posted on 2008-06-18
19
300 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 - Access MVP) 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
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…
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…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

762 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

18 Experts available now in Live!

Get 1:1 Help Now