Results 1 to 9 of 9

Thread: Check If Dir Exists

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    I currently use this code to see if a file exists:

    Public Function FileExist(sData As String) As Boolean
    On Error Resume Next
    If sData <> "" Then
    File = Len(Dir$(sData))
    If Err Or File = 0 Then
    FileExist = False
    Else
    FileExist = True
    End If
    End If
    End Function

    but if i want to use it to see if a directory exists it never works, is there a proper way to do this ?

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Code:
    Private Const MAX_PATH = 260
    Private Const INVALID_HANDLE_VALUE = -1
    Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
     
    Private Type FILETIME
       dwLowDateTime As Long
       dwHighDateTime As Long
    End Type
    
    Private Type WIN32_FIND_DATA
       dwFileAttributes As Long
       ftCreationTime As FILETIME
       ftLastAccessTime As FILETIME
       ftLastWriteTime As FILETIME
       nFileSizeHigh As Long
       nFileSizeLow As Long
       dwReserved0 As Long
       dwReserved1 As Long
       cFileName As String * MAX_PATH
       cAlternate As String * 14
    End Type
    
    Private Declare Function FindFirstFile Lib "kernel32" _
       Alias "FindFirstFileA" _
      (ByVal lpFileName As String, _
       lpFindFileData As WIN32_FIND_DATA) As Long
       
    Private Declare Function FindNextFile Lib "kernel32" _
       Alias "FindNextFileA" _
      (ByVal hFindFile As Long, _
       lpFindFileData As WIN32_FIND_DATA) As Long
    
    Private Declare Function FindClose Lib "kernel32" _
      (ByVal hFindFile As Long) As Long
    
    
    Private Function FolderExists(sFolder As String) As Boolean
    
       Dim hFile As Long
       Dim WFD As WIN32_FIND_DATA
       
      'remove training slash before verifying
       sFolder = UnQualifyPath(sFolder)
    
      'call the API pasing the folder
       hFile = FindFirstFile(sFolder, WFD)
       
      'if a valid file handle was returned,
      'and the directory attribute is set
      'the folder exists
       FolderExists = (hFile <> INVALID_HANDLE_VALUE) And _
                      (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY)
       
      'clean up
       Call FindClose(hFile)
       
    End Function
    
    
    Private Function UnQualifyPath(ByVal sFolder As String) As String
    
      'trim and remove any trailing slash
       sFolder = Trim$(sFolder)
       
       If Right$(sFolder, 1) = "\" Then
             UnQualifyPath = Left$(sFolder, Len(sFolder) - 1)
       Else: UnQualifyPath = sFolder
       End If
       
    End Function
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Junior Member
    Join Date
    Oct 2000
    Posts
    17

    Cool If You Want To Make The Dir is Its No There

    on error goto matt
    'Do What Ever You Want With the Dir
    matt:
    mkdir ("c:\Yourfolder")

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341

    Thumbs up

    thanks/

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Shoudn't be that hard, dir's working as it should:

    cbool(len(dir("C:\windows",vbDirectory )))
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    Guest
    Code:
    If Dir("C:\Windows", vbDirectory) <> "" Then MsgBox ("Dir exists")

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    Thats what i was using except that it never worked , the code i have now does though.

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    should work! Maybe you put an extra "\" after the path?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    no silly me just did not put 'vbDirectory'

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