How to retrieve the Directory name

I want a routine which check if the directory “my Dir” exists or not if  not then it creates it.
fmuftiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

watyCommented:
if DirExists("MyDir") = False then MkDir "MyDir"

' #VBIDEUtils#************************************************************
' * Programmer Name  : Shane Arnfield
' * Web Site         : http://www.freevbcode.com/ShowCode.Asp?ID=501
' * E-Mail           : waty.thierry@usa.net
' * Date             : 23/11/1999
' * Time             : 10:38
' **********************************************************************
' * Comments         : Determine if a Directory exists
' *
' *
' **********************************************************************
Function DirExists(ByVal sDName As String) As Boolean

   Dim sDummy As String

   On Error Resume Next

   If Right(sDName, 1) <> "\" Then sDName = sDName & "\"
   sDummy = Dir$(sDName & "*.*", vbDirectory)
   DirExists = Not (sDummy = "")

End Function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
watyCommented:
You could also use the following to create the folder :

' #VBIDEUtils#************************************************************
' * Programmer Name  : Brock Weaver
' * Web Site         : http://www.freevbcode.com/ShowCode.Asp?ID=257
' * E-Mail           : waty.thierry@usa.net
' * Date             : 24/09/1999
' * Time             : 11:59
' **********************************************************************
' * Comments         : Create Path Recursively as Needed
' *
' * If your app needs to create a file somewhere and it is possible
' * for your user type in the path (such as in a common dialog)
' * this little guy will let you keep from bombing out by creating
' * needed directories on the fly it will NOT alter any present directories.
' *
' **********************************************************************
Public Function CreateFolder(sFolder As String) As String
   On Error GoTo ErrorHandler
   Dim s As String
   s = GetPathOnly(sFolder)
   If Dir(s, vbDirectory) = "" Then
      s = CreateFolder(s)
      MkDir s
   End If
   CreateFolder = sFolder
   exit function

ErrorHandler:
   exit function
End Function

Public Function GetPathOnly(sPath As String) As String
   GetPathOnly = Left(sPath, InStrRev(sPath, "\", Len(sPath)) - 1)
End Function
0
fmuftiAuthor Commented:
I want to check for four directories even if any sigle one is not there the routine should create it, however if all four exists then not create again.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

watyCommented:
You could create them each time, and resume next for the error, so you don't need to check :)
0
fmuftiAuthor Commented:
If I create each time the data there in the folders will be deleted. Infact I need this once I install my Software on any system. But if this routine is there I dont have to worry on how many system I install my application. Hope now u'll find me the solution.
0
watyCommented:
You could create it each time, it won't delete the files in the folder.

If the folder already exists, it will generate an error, and just ignore it :)
0
fmuftiAuthor Commented:
I'll let u know tommorrow.
0
fmuftiAuthor Commented:
I have used this approach but Iit doesn’t seem to be elegant. Can u restructure it to make it smaller(something like looping….). Because there are too many If -Else
Public Sub DirExists()
Dim Exists As String
    Exists = Dir(App.Path & "\" & "INITIAL", vbDirectory)
    If Exists <> "INITIAL" Then
    MkDir App.Path & "\INITIAL"
    End If
    Exists = Dir(App.Path & "\" & "FINAL", vbDirectory)
    If Exists <> "FINAL" Then
    MkDir App.Path & "\FINAL"
    End If
    Exists = Dir(App.Path & "\" & "RESULT", vbDirectory)
    If Exists <> " RESULT " Then
    MkDir App.Path & "\ RESULT "
    End If
    Exists = Dir(App.Path & "\" & "TERMINATE", vbDirectory)
    If Exists <> " TERMINATE " Then
    MkDir App.Path & "\ TERMINATE "
    End If
End Sub
0
watyCommented:
Public Sub DirExists()
   On error resume next
   MkDir App.Path & "\INITIAL"
   MkDir App.Path & "\FINAL"
   MkDir App.Path & "\ RESULT " 
   MkDir App.Path & "\ TERMINATE " 
End Sub
0
fmuftiAuthor Commented:
Thanx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.