Existing directory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bo Wisén

    Existing directory

    If I want to decide if a file exists I can use 'if
    dir("c:\test1\t est2\testfile.t xt")="testfile. txt"'.

    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?

    / Bo


  • J French

    #2
    Re: Existing directory

    On Sun, 21 Nov 2004 09:29:08 GMT, "Bo Wisén" <bowisen@telia. com>
    wrote:
    [color=blue]
    >If I want to decide if a file exists I can use 'if
    >dir("c:\test1\ test2\testfile. txt")="testfile .txt"'.[/color]

    No you don't - that is horrible
    - and very likely to give you problems
    [color=blue]
    >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?[/color]

    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\US LIB.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(FileSpe c$, Erm$)
    Dim S$

    Erm$ = ""
    If InStr(FileSpec$ , "\") Then
    S$ = ExtractFilePath (FileSpec$)
    Call MakeDir(S$, Erm$)
    If Len(Erm$) = 0 Then
    If DirExists(FileS pec$) = 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\test 2"
    Call MakeDir(FileSpe c$, Erm$)
    MsgBox Erm$
    End Sub

    Comment

    Working...