Check Path

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • build

    Check Path

    G'day All,
    I'm using code below to check a path selected by a user.
    Can anyone pick holes in it please?
    The more critical you are the better.
    tia
    build

    'GET SOURCE PATH --------------------------------------
    inPath = Me.txtPath.Text
    'CHECK PATH ----------------------------
    If inPath = "" Then
    'ToDo: Fix properly!
    strTMP = "Select a bloody path, you idiot!"
    sknMsg strTMP, 0
    Exit Sub
    Else
    If flgFolder = True Then
    If folderExists(in Path) Then
    strTMP = "*.txt"
    If Dir(inPath & "\" & strTMP) = "" Then
    strTMP = "No text files in: " & inPath
    sknMsg strTMP, 0
    Exit Sub
    End If
    Else
    strTMP = "Can't find: " & inPath
    End If
    Else
    If Dir(inPath) = "" Then
    strTMP = "Can't find: " & inPath
    sknMsg strTMP, 0
    Exit Sub
    End If
    End If
    End If

    Public Function folderExists(in Path As String) As Boolean
    'folder doesn't exist
    If Dir(inPath, vbDirectory) = vbNullString Then
    folderExists = False
    Else
    folderExists = True
    End If
    End Function

    Public Function fileExist(inPat h As String) As Boolean
    If UCase(Dir(inPat h)) = UCase(TrimPath( inPath)) Then
    fileExist = True
    Else
    fileExist = False
    End If
    End Function

    Public Function TrimPath(ByVal inPath As String) As String
    If Len(inPath) = 0 Then Exit Function
    Dim i As Integer
    Do
    i = InStr(inPath, "\")
    If i = 0 Then Exit Do
    inPath = Right(inPath, Len(inPath) - i)
    Loop
    TrimPath = inPath
    End Function
  • J French

    #2
    Re: Check Path

    I really would look over that code again.

    Try re-writing it so that you do not have a single 'Else' in the
    routine

    Also break out the logic
    - you should do your explicit test for FolderExists just once
    - and if it is there you then do the (optional) test for *.TXT

    Look at what happens if flgFolder = True but the directory does not
    exist - it sets strTmp and does nothing with it

    Do NOT use Dir() in FileExists and FolderExists
    - it will work ok here but one day will bite you
    - use GetAttr with Error Trapping

    I don't understand why you write 'folderExists' rather that
    'FolderExists' - I suppose you have a reason, but to me it looks
    peculiar - ' inStr() ' 'fileLen()' .... nah ....

    Being old fashioned I call a Directory a 'Directory' - not a 'Folder'

    I also dislike excessively long names like strTMP
    - personally I reserve S$ for 'dragonfly' Strings
    - or give things a meaningful name like Msg$
    Ok, you could say you like Hungarian notation, but in that case why do
    you have 'inPath' rather than strInPath ?

    sknMsg always seems to have 0 as its second parameter
    It might be an idea to look at Optional parameters
    - or to have a helper Sub that wraps the real sknMsg
    - actually I reckon the error message should be passed out of the sub,
    not displayed from within it

    Looking at your function TrimPath()
    What happens if they enter: 'c:\windows\sys tem\testdata
    - actually the mind boggles trying to trace the result
    What is the function supposed to do ?
    Since it is not blindingly obvious it should be documented
    Also, why is it not 'trimPath' ?

    From the Exit Subs one can tell that the first bit of code is a Sub
    - where is the End Sub ?
    - and for that matter, where are the entry parameters ?

    Is flgFolder a parameter ?
    I can't see how you pass out an indication whether the Sub suceeded or
    failed.
    I'm also not keen on the way you hard coded in *.TXT that will bite
    you in the butt if you decide to use this routine for *.DAT

    I suggest that you re-write this properly and post it again.

    On the plus side you had the courage to post it here and ask for
    criticism - and you will learn a lot from that

    On Thu, 21 Oct 2004 17:36:59 +1000, build
    <build02DELTHIS @datafast.net.a u> wrote:
    [color=blue]
    >G'day All,
    >I'm using code below to check a path selected by a user.
    >Can anyone pick holes in it please?
    >The more critical you are the better.
    >tia
    >build
    >
    >'GET SOURCE PATH --------------------------------------
    >inPath = Me.txtPath.Text
    >'CHECK PATH ----------------------------
    >If inPath = "" Then
    > 'ToDo: Fix properly!
    > strTMP = "Select a bloody path, you idiot!"
    > sknMsg strTMP, 0
    > Exit Sub
    >Else
    > If flgFolder = True Then
    > If folderExists(in Path) Then
    > strTMP = "*.txt"
    > If Dir(inPath & "\" & strTMP) = "" Then
    > strTMP = "No text files in: " & inPath
    > sknMsg strTMP, 0
    > Exit Sub
    > End If
    > Else
    > strTMP = "Can't find: " & inPath
    > End If
    > Else
    > If Dir(inPath) = "" Then
    > strTMP = "Can't find: " & inPath
    > sknMsg strTMP, 0
    > Exit Sub
    > End If
    > End If
    >End If
    >
    >Public Function folderExists(in Path As String) As Boolean
    >'folder doesn't exist
    >If Dir(inPath, vbDirectory) = vbNullString Then
    > folderExists = False
    >Else
    > folderExists = True
    >End If
    >End Function
    >
    >Public Function fileExist(inPat h As String) As Boolean
    >If UCase(Dir(inPat h)) = UCase(TrimPath( inPath)) Then
    > fileExist = True
    >Else
    > fileExist = False
    >End If
    >End Function
    >
    >Public Function TrimPath(ByVal inPath As String) As String
    >If Len(inPath) = 0 Then Exit Function
    >Dim i As Integer
    >Do
    > i = InStr(inPath, "\")
    > If i = 0 Then Exit Do
    > inPath = Right(inPath, Len(inPath) - i)
    >Loop
    >TrimPath = inPath
    >End Function[/color]

    Comment

    • Steve Gerrard

      #3
      Re: Check Path


      "J French" <erewhon@nowher e.com> wrote in message
      news:417784c8.3 32704676@news.b tclick.com...
      |
      | Do NOT use Dir() in FileExists and FolderExists
      | - it will work ok here but one day will bite you
      | - use GetAttr with Error Trapping
      |

      Why? What is the snag in Dir()? Unless you are talking about the
      problematic use of Dir to enumerate files, using Dir("*.tmp") followed
      by Dir() in a loop. I don't use Dir that way, and haven't had problems
      with Dir for ordinary verification.


      Comment

      • J French

        #4
        Re: Check Path

        On Thu, 21 Oct 2004 18:19:23 -0700, "Steve Gerrard"
        <mynamehere@com cast.net> wrote:
        [color=blue]
        >
        >"J French" <erewhon@nowher e.com> wrote in message
        >news:417784c8. 332704676@news. btclick.com...
        >|
        >| Do NOT use Dir() in FileExists and FolderExists
        >| - it will work ok here but one day will bite you
        >| - use GetAttr with Error Trapping
        >|
        >
        >Why? What is the snag in Dir()? Unless you are talking about the
        >problematic use of Dir to enumerate files, using Dir("*.tmp") followed
        >by Dir() in a loop. I don't use Dir that way, and haven't had problems
        >with Dir for ordinary verification.[/color]

        He is using it in DirExists() and FileExists()
        - potentially deeply buried utilities

        Comment

        Working...