|
-
Jul 17th, 2000, 11:29 AM
#1
In my program, I have to check if a particular directory exists or not. If not, it would have to be created.
Example c:\abc\123
I would have to first check if c:\abc exists. If not create c:\abc and then create c:\abc\123.
Does anybody have a piece of code which would do this?
Thanks.
-
Jul 17th, 2000, 11:46 AM
#2
Fanatic Member
Try this ...
Code:
Dim strPath As String
Dim fso As FileSystemObject
strPath = "c:\abc"
' create file system object
Set fso = New FileSystemObject
' Create folder if it does not exist
If Not fso.FolderExists(strPath) Then
fso.CreateFolder strPath
fso.CreateFolder strPath & "\123"
End If
Set fso = Nothing
Only works for VB6 though.
Hope it helps
-
Jul 17th, 2000, 12:02 PM
#3
Fanatic Member
...or try this:
Code:
On Error Resume Next
If Right$(stDirectory, 1) <> "\" Then
strDirName = stDirectory & "\"
End If
stFile = Dir$(stDirectory & "*.*", vbDirectory)
DirExists = Not (stFile = vbNullString)
Err = 0
-
Jul 17th, 2000, 12:09 PM
#4
This is the code I got from this site. This makes sure that
the entire path is created.
If Len(Dir$(strDirectory, vbDirectory)) <= 0 Then
Dim Fpath As String
Dim Branches() As String
Dim b As Integer
Branches = Split(strDirectory, "\")
For b = LBound(Branches) To UBound(Branches)
Fpath = Fpath & Branches(b) & "\"
If Len(Dir$(Fpath, vbDirectory)) <= 0 Then
MkDir Fpath
End If
Next b
End If
-
Jul 17th, 2000, 12:24 PM
#5
_______
<?>
Code:
'Check if directory exists
'-----------------------------------
Public Function DirExists(ByVal sDirName As String) As Boolean
Dim sDir As String
On Error Resume Next
DirExists = False
sDir = Dir$(sDirName, vbDirectory)
If (Len(sDir) > 0) And (Err = 0) Then
DirExists = True
End If
End Function
Private Sub Command1_Click()
Dim x As Boolean
x = False
x = DirExists("Directory in question path!")
If DirExists = False Then MkDir "C:\mynewdir"
Else
'open your file and do whatever...etc.
End If
End Sub
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Jul 17th, 2000, 12:34 PM
#6
Thanks for all your replies.
As I mentioned earlier, apart from checking and creating the directory in question, I need to make sure that the parent folder exists.(This needs to be done recursively until the drive is found and if not create the parent folder too.
The code I got from one of the previous posts does just that part.
-
Jul 25th, 2000, 09:24 PM
#7
New Member
Try this ...
-------------------------
Dim MyPath As String
MyPath = "c:\whatever\dude"
If Dir(MyPath, vbDirectory) = "" Then
MakeFullDir (MyPath)
End If
-------------------------
Function MakeFullDir(d As String) As Boolean
Dim I As Integer
Dim dc As New Collection
Dim dname As String
On Error goto ErrHdler
Do While Dir(d, vbDirectory) = ""
dname = DeepestDir(d)
dc.Add dname
d = Left$(d, Len(d) - Len(dname))
Loop
For I = dc.Count To 1 Step -1
d = d & dc(I) & "\"
MkDir d
Next
Set dc = Nothing
MakeFullDir = True
Exit Function
ErrHdler:
Set dc = Nothing
MakeFullDir = False
End Function
-------------------------
Function DeepestDir(d As String) As String
If Right$(d, 1) = "/" Or Right$(d, 1) = "\" Then d = Left$(d, Len(d) - 1)
DeepestDir = GetFileNamefromFullPath(d)
End Function
-------------------------
Function GetFileNamefromFullPath(fp As String) As String
Dim l As Integer
l = Len(fp)
If l > 0 Then
Do While l > 1
If (Mid$(fp, l, 1) = "/" Or Mid$(fp, l, 1) = "\") Then Exit Do
l = l - 1
Loop
End If
If l > 0 Then
GetFileNamefromFullPath = Mid$(fp, l + 1)
Else
GetFileNamefromFullPath = fp
End If
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|