On Sun, 21 Nov 2004 09:29:08 GMT, "Bo Wisén" <bo*****@telia.com>
wrote:
If I want to decide if a file exists I can use 'if
dir("c:\test1\test2\testfile.txt")="testfile.txt" '.
No you don't - that is horrible
- and very likely to give you problems
But now I want my application to decide if a directory exists and, if not,
create it. Maybe there's a hard way with an error handler searching for
error codes but is there a simplier way?
You'll find what you need in here
' ---
Function FileExists(Fle$) As Boolean
Dim Q%
On Error Resume Next
Q = GetAttr(Fle$)
If Err = 0 Then
If (Q And vbDirectory) = 0 Then
FileExists = True
End If
End If
Err.Clear
End Function
' ---
Function DirExists(ADir$) As Boolean
Dim Q%
On Error Resume Next
Q = GetAttr(ADir$)
If Err = 0 Then
If (Q And vbDirectory) = vbDirectory Then
DirExists = True
End If
End If
Err.Clear
End Function
'
################################################## #######################
'
' C:\DEV\USLIB\USLIB.BAS --> X:\DEV\USLIB
'
Function ExtractFilePath$(Fle$)
Dim L9%
For L9 = Len(Fle$) To 1 Step -1
If InStr(":\", Mid$(Fle$, L9, 1)) Then
If Mid$(Fle$, L9, 1) = "\" Then
ExtractFilePath$ = Left$(Fle$, L9 - 1)
End If
If Mid$(Fle$, L9, 1) = ":" Then
ExtractFilePath$ = Left$(Fle$, L9)
End If
L9 = 1
End If
Next
End Function
Sub MakeDir(FileSpec$, Erm$)
Dim S$
Erm$ = ""
If InStr(FileSpec$, "\") Then
S$ = ExtractFilePath(FileSpec$)
Call MakeDir(S$, Erm$)
If Len(Erm$) = 0 Then
If DirExists(FileSpec$) = False Then
On Error Resume Next
MkDir FileSpec$
If Err Then Erm$ = "Error Making " + FileSpec$
On Error GoTo 0
End If
End If
End If
End Sub
Private Sub Command2_Click()
Dim Erm$, FileSpec$
' note the deliberate error
FileSpec$ = "c:\t/test\test1\test2"
Call MakeDir(FileSpec$, Erm$)
MsgBox Erm$
End Sub