Results 1 to 6 of 6

Thread: Short QnD way to determine if it's a file or a folder

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Short QnD way to determine if it's a file or a folder

    Put a DirListBox on your Form then in the code:

    Code:
      '
      '
    On Error Resume Next
    
    Dir1.Path = ItemName
               
    If Err.Number = 76 Then
      ' It's a file
    Else
      ' It's a directory
    End If
      '
      '


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Short QnD way to determine if it's a file or a folder

    Here's an alternate method that doesn't require a control:

    Code:
        On Error GoTo 1
        If (GetAttr(PathName) And vbDirectory) <> vbDirectory Then
            'It's a file
        Else
            'It's a directory
    1   End If 'If GetAttr raised an error, PathName doesn't exist
        On Error GoTo 0
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Short QnD way to determine if it's a file or a folder

    Even better, Bonnie.

    Question:

    Is this a good way to determine file or folder or should one use an API to do this
    Last edited by jmsrickland; Nov 27th, 2015 at 12:35 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  4. #4
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Short QnD way to determine if it's a file or a folder

    Quote Originally Posted by jmsrickland View Post
    Question:

    Is this a good way to determine file or folder or should one use an API to do this
    It's the quickest and simplest built-in way that I know of. You'd only want to directly call the underlying API (GetFileAttributes) if you need a Unicode-aware solution and/or require the fastest method.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  5. #5
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,708

    Re: Short QnD way to determine if it's a file or a folder

    Does PathIsDirectory use GetFileAttributes?

  6. #6
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Short QnD way to determine if it's a file or a folder

    Quote Originally Posted by fafalone View Post
    Does PathIsDirectory use GetFileAttributes?
    API Monitor detected that PathIsDirectory does indeed call GetFileAttributes.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width