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?
 
watyConnect With a Mentor Commented:
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
 
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
All Courses

From novice to tech pro — start learning today.