Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Create all the sub directories of specified path.

Total Hit ( 2184)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Sometimes we need to create a directory if it doesn't exist. It is easy to create directories using VB. You can use MkDir function. But what if we need to create all directories of specified path if none of them exists? Well, if you are running Win2K or higher then its easy. You can use following code which is the easiest solution (but this is not supported on win 3.1/9x/ME so be careful)

Click here to copy the following block
Private Declare Function SHCreateDirectoryEx Lib "shell32" Alias "SHCreateDirectoryExA" (ByVal hwnd As Long, ByVal pszPath As String, ByVal psa As Any) As Long
Private Sub Form_Load()
  SHCreateDirectoryEx Me.hwnd, "c:\dir1\dir2\", ByVal 0&
End Sub

If this doesn't work then what is the solution...?

.... Here is the solution for Win9x/Me etc. using API.

Before running VBCreateAllDir function



After running VBCreateAllDir function



Click here to copy the following block
'API declaration
Private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, ByVal lpSecurityAttributes As Long) As Long

Private Function GetdirNamefromfilename(ByVal thefullfilename As String) As String
Dim temp
Dim temp1 As String
Dim i
temp1 = ""
temp = Split(CStr(thefullfilename), "\")
For i = 0 To UBound(temp) - 1
temp1 = temp1 & temp(i) & "\"
Next i
GetdirNamefromfilename = temp1
End Function


Private Function VBCreateAllDir(ByVal thedir As String)
Dim temp
Dim temp1 As String
Dim i
Dim retval As Long
VBCreateAllDir = True

thedir = Trim(thedir)
If Right(thedir, 1) = "\" Then thedir = Left(thedir, Len(thedir) - 1)
temp = Split(thedir, "\")
temp1 = temp(0)
For i = 1 To UBound(temp)
temp1 = temp1 & "\" & temp(i)
If CreateDirectory(temp1 & Chr(0), 0&) = 0 Then
 If i = UBound(temp) Then
  VBCreateAllDir = False
 End If
End If
Next i

End Function

Private Sub Form_Load()

'//Using Full File Path
'VBCreateAllDir (GetdirNamefromfilename("C:\Root\Dir1\Dir2\Dir3\myfile.txt"))

'//Using Only Dir Path
VBCreateAllDir ("C:\Root\Dir1\Dir2\Dir3\")

End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.