Link to home
Start Free TrialLog in
Avatar of ellenjbr
ellenjbr

asked on

Access 2002 - Create a new directory

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!
ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ellenjbr
ellenjbr

ASKER

Great!  Thanks!
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

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!

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
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.
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
boag2000,

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

JeffCoachman
The code I posted is in daily use - with no errors.

mx
I'm confused ... is there an issue?

mx
MX,

I'm not sure, see post:
https://www.experts-exchange.com/questions/23497685/Access-2002-Create-a-new-directory.html?anchorAnswerId=21819953#a21819953
< Also, on testing the previous code, it errored after the folder was created>

Let's wait and see.

Jeff
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
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!
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
Your code works fine ... thanksl
Yes ... I'm using it in another app as we 'speak'.

mx