|
|
|
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) |
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 | 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()
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 ) |
|
|