Results 1 to 20 of 20

Thread: testing a path

  1. #1

    Thread Starter
    Lively Member Crazy_bee's Avatar
    Join Date
    Jul 2001
    Location
    Fitchburg
    Posts
    90

    Arrow testing a path

    Can anyone tell me how I can test to make sure a directory exists?
    Thank You
    Don't ever Ginop before you Ginip

  2. #2
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    VB Code:
    1. Private Sub Command1_Click()
    2.     If Dir("c:\temp", vbDirectory) <> "" Then
    3.         MsgBox "Directory exists"
    4.     End If
    5. End Sub

  3. #3
    Addicted Member
    Join Date
    Jul 2001
    Location
    Minnesota, USA
    Posts
    134
    Create a new FileSystemObject then use myFSO.FolderExists

    Straight from the MSDN, so I haven't tested it.
    The answer is 42. Sadly, you can't typecast the "answer" data type to the "question" data type.

    Code:
    Take.Me( To( Your.Leader ) )

  4. #4
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    You gotta be kidding me. You cant be asking him to add 255K to his project for the FSO object just to check if a dir exists. Heres a function i wrote (after much debate):

    VB Code:
    1. Function DirExist(ByVal DirPath As String) As Boolean
    2.     On Error Resume Next
    3.     'The above line is only required on Win NT4, and 2K. Remove on Win9x
    4.     If Right$(DirPath, 1) <> "\" Then DirPath = DirPath & "\"
    5.     DirExist = Dir$(DirPath, vbDirectory) <> ""
    6. End Function
    The on error resume next is there cause on WinNT and 2K, an error occurs if you point the function to a file. Not required in Win9x.If you want the whole darn debate, here it is.
    Last edited by nishantp; Aug 31st, 2001 at 10:48 AM.
    You just proved that sig advertisements work.

  5. #5
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    255k? where did you get your dll? it's only 145k or so. and not that bad. both work, keep negative comments like that out of it. Alternate methods, that's all there is to it.
    -Excalibur

  6. #6
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    OH its only 145K? Sorry. Anyways the code in that function is 269 bytes!..so beat that! And thats with the comments!

    FSO = Evil
    You just proved that sig advertisements work.

  7. #7
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Yeah, not bad - nishantp's is the best attempt so far, this is the fastest way (as was proved by some gurus on this site ages ago with nothing better to do than test this ) ...
    Code:
    If (Len$(Dir("C:\YourPathHere\")) <> 0) Then
        Msgbox "Yep, it's here alright !"
    End If
    So there ! pphhhtttt

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  8. #8
    Originally posted by Skitchen8



    agreed
    Read my signature.

  9. #9
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Sorry,
    I have signatures turned off right now, and am too lazy to turn them back on.


    -Lou

    heh,heh:

    Originally posted by filburt1
    I have sigs off and I am too lazy to turn them on.
    as seen HERE:
    http://www.vbforums.com/showthread.p...threadid=95057

  10. #10
    Arien Talabac
    The 100,000th poster of Chit Chat
    Member of the MSN Replacement development team
    Comment on PassProg in preparation for the next major release!
    These elements of VB are members of an evil cult:
    • FileSystemObject
    • Variant
    • Global
    • Anything like Form1, Text4, Command14, Image3, etc.


  11. #11
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728

    Talking

    it's only 145kb
    Thats 145kb too much for my liking

    Laterz
    Digital-X-Treme
    Contact me on MSN Messenger: [email protected]

    [VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
    / (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]

  12. #12
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Originally posted by alex_read
    Yeah, not bad - nishantp's is the best attempt so far, this is the fastest way (as was proved by some gurus on this site ages ago with nothing better to do than test this ) ...
    Code:
    If (Len$(Dir("C:\YourPathHere\")) <> 0) Then
        Msgbox "Yep, it's here alright !"
    End If
    So there ! pphhhtttt
    I c. I suppose that would be cause a numberical comparison as faster than a string comparison. Ill do some tests.
    You just proved that sig advertisements work.

  13. #13
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Ok i did a test involving a loop of 200 done 100 times and i took the average time. It seems that useing the Len(dir) method and the numerical comparison is slighly faster. Very slightly. My a few milliseconds. I did this test on a P450 with 64MB RAM.

    Also, i have Win2K and Fat32 (yes there is a reason for that). On my machine, this works also:
    VB Code:
    1. Function DirExist(ByVal DirPath As String) As Boolean
    2.     On Error Resume Next
    3.     'The above line is only required on Win NT4, and 2K. Remove on Win9x
    4.     DirExist = Len(Dir$(DirPath, vbDirectory)) <> 0
    5. End Function
    It would seem that the "\| isnt necessary at the end of the dir path. Could someone with Win9x please test this also? And perhaps some people with NTFS? I would like to once and for all optomize this function.
    Last edited by nishantp; Aug 31st, 2001 at 05:15 PM.
    You just proved that sig advertisements work.

  14. #14
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Originally posted by nishantp

    VB Code:
    1. Function DirExist(ByVal DirPath As String) As Boolean
    2.     On Error Resume Next
    3.     'The above line is only required on Win NT4, and 2K. Remove on Win9x
    4.     DirExist = Len(Dir$(DirPath, vbDirectory)) <> 0
    5. End Function
    It would seem that the "\| isnt necessary at the end of the dir path. Could someone with Win9x please test this also? ....
    ? I would like to once and for all optomize this function.
    Win 9X Reporting in, SIR!
    Satus Report:

    Works Perfectly, And for consistency, I recommend leaving
    the On Error Resume Next, so as to not confuse people,
    if it turns out to work everywhere.

    BTW, Good Code, All!

    {Just to be different, a method could be built around
    the Name function {{Name DirPath As DirPath}}, and when an error is raised, that means the dir didn't exist, BUT, heh, why have
    five or six lines, when 1 or 2 will do it much more quickly?}


    -Lou

  15. #15
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Originally posted by nishantp
    OH its only 145K? Sorry. Anyways the code in that function is 269 bytes!..so beat that! And thats with the comments!

    FSO = Evil
    Evil ???? Boy, I hate religious fanatics. Root of 99% of the problems in the world. The other 1% is Bill Gates fault.

    269 bytes with a run-time dependency that STARTS at 1.35MB. Imagine that. I bet all the C++ and Delphi programmers must be shaking in their undies right now.

    Following is lame but it works.

    VB Code:
    1. On Error Resume Next
    2. ChDir "c:\temp"
    3. If Err.Number <> 0 Then
    4.    MsgBox "folder not found"
    5. End If

  16. #16
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Even worse: Any one know how to keep SHFileOperation from popping up the confirmation dialog ?? I know it's a flag of some sort but can't find it.

    VB Code:
    1. 'In general section
    2. Private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
    3. Private Type SECURITY_ATTRIBUTES
    4.     nLength As Long
    5.     lpSecurityDescriptor As Long
    6.     bInheritHandle As Long
    7. End Type
    8. Private Type SHFILEOPSTRUCT
    9.     hWnd As Long
    10.     wFunc As Long
    11.     pFrom As String
    12.     pTo As String
    13.     fFlags As Integer
    14.     fAborted As Boolean
    15.     hNameMaps As Long
    16.     sProgress As String
    17. End Type
    18. Private Const FO_DELETE = &H3
    19. Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    20. Private Sub Command1_Click()
    21.     If FolderExist("c:\bob") = True Then
    22.         MsgBox "it's there"
    23.     Else
    24.         MsgBox "Boo-Hoo"
    25.     End If
    26. End Sub
    27.  
    28. Public Function FolderExist(sFolderPath) As Boolean
    29.     'KPD-Team 1998
    30.     'URL: [url]http://www.allapi.net/[/url]
    31.     'E-Mail: [email][email protected][/email]
    32.     Dim Security As SECURITY_ATTRIBUTES
    33.     'Create a directory
    34.     Ret& = CreateDirectory(sFolderPath, Security)
    35.     'If CreateDirectory returns 0, the function has failed
    36.     If Ret& = 0 Then
    37.         FolderExist = True
    38.     Else
    39.         Dim SHDirOp As SHFILEOPSTRUCT
    40.         With SHDirOp
    41.             .wFunc = FO_DELETE
    42.             .pFrom = sFolderPath
    43.         End With
    44.         'Delete the directory
    45.         SHFileOperation SHDirOp
    46.        
    47.         FolderExist = False
    48.     End If
    49. End Function

  17. #17
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Never mind. Found the RemoveDirectory API call.

    VB Code:
    1. If Ret& = 0 Then
    2.       FolderExist = True
    3. Else
    4.       RemoveDirectory sFolderPath
    5.       FolderExist = False  
    6. End If


    Hmmmm......how about a FolderExists function that scans the whole drive getting all folders looking to see if it's there ??

    How in-efficent can I make this function ???

  18. #18
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    FSO to the rescue.


    VB Code:
    1. Option Explicit
    2. Private fso As FileSystemObject
    3. Private aPath() As String
    4. Private lPathCount As Long
    5.  
    6. Public Sub GetTheFiles(ByVal sFolderToSearch As String)
    7.     Dim fsoFolder As Folder
    8.    
    9.     ' get sub-folders in current folder
    10.     Set fsoFolder = fso.GetFolder(sFolderToSearch)
    11.        
    12.     ' loop thru all sub-flders
    13.     If fsoFolder.SubFolders.Count > 0 Then
    14.          For Each fsoFolder In fsoFolder.SubFolders
    15.             If (lPathCount Mod 500) = 0 Then
    16.                 ReDim Preserve aPath(0 To UBound(aPath) + 500)
    17.             End If
    18.             aPath(lPathCount) = fsoFolder.Path
    19.             lPathCount = lPathCount + 1
    20.             ' recurse thru them
    21.             Call GetTheFiles(fsoFolder.Path)
    22.             DoEvents
    23.          Next
    24.     End If
    25.     Exit Sub
    26.  
    27. End Sub
    28.  
    29. Public Function FolderExists(sPath) As Boolean
    30. Dim s As String
    31. Dim l As Long
    32.     MousePointer = vbHourglass
    33.    
    34.     Call GetTheFiles(Left$(sPath, 3))
    35.    
    36.     ReDim Preserve aPath(0 To lPathCount - 1)
    37.    
    38.     FolderExists = False
    39.     For l = 0 To UBound(aPath)
    40.         If UCase$(aPath(l)) = UCase$(sPath) Then
    41.             FolderExists = True
    42.             Exit For
    43.         End If
    44.     Next
    45.        
    46.     MousePointer = vbDefault
    47.    
    48.     Erase aPath
    49.  
    50. End Function
    51.  
    52. Private Sub Command1_Click()
    53.     If FolderExists("c:\john") = True Then
    54.         MsgBox "It's there"
    55.     Else
    56.         MsgBox "Boo-Hoo"
    57.     End If
    58.    
    59. End Sub
    60.  
    61. Private Sub Form_Load()
    62.     Set fso = New FileSystemObject
    63.     ReDim aPath(0 To 500)
    64.     lPathCount = 0
    65. End Sub

  19. #19
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Originally posted by Patoooey


    Evil ???? Boy, I hate religious fanatics. Root of 99% of the problems in the world. The other 1% is Bill Gates fault.

    269 bytes with a run-time dependency that STARTS at 1.35MB. Imagine that. I bet all the C++ and Delphi programmers must be shaking in their undies right now.

    Following is lame but it works.

    VB Code:
    1. On Error Resume Next
    2. ChDir "c:\temp"
    3. If Err.Number <> 0 Then
    4.    MsgBox "folder not found"
    5. End If
    Were not actually religious fanatics you know BTW, with FSO...you would jus be adding 145K to that 1.35 MB. Doesnt do much good.
    You just proved that sig advertisements work.

  20. #20
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Originally posted by nishantp
    Were not actually religious fanatics you know BTW, with FSO...you would jus be adding 145K to that 1.35 MB. Doesnt do much good.
    OK..granted on the religious part. You guys are not as nutz as the bible-thumpers. Your still nutz....just not as bad. <g>

    AND !!!...if you compress the FSO DLL...it's now a mere 61kb. Hell, even at 56kb that's only a few seconds at most to download. After sitting thru the "for-ever" download of the run-time, a few more seconds is no problem.

    And the 1.3MB is a minimum. Never tried to see what is possible with only that one file but I bet it ain't much. Hmmm....wonder what you can do with just that one file.....Naw...I'm not screwing up my system to test it out. hehehehehe

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