Results 1 to 5 of 5

Thread: ow do you check if a directory exists

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Location
    Wellington NZ
    Posts
    153

    Talking

    hi there anyone know how to check if a directory is on a drive. I am looking for "C:\bakup" and if it is no found the create the directory?

    Rohan West

  2. #2
    Member
    Join Date
    Jun 2000
    Location
    North of France
    Posts
    49

    Lightbulb

    I don't know if there a possibility to test that. But try to create another directory which has the same name. If the MkDir function return an error (Err 58: already exist), you've got you're answer !

    PS: I've just see that DirExists does what you want. Thanks to HeSaidJoe

    [Edited by (B2F)Tom on 07-11-2000 at 08:52 AM]
    Hi-Tech Engineer

  3. #3
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    here you are:

    Code:
    Private Sub Command1_Click()
      If Dir("C:\bakup", vbDirectory) = "" Then
        MkDir "C:\bakup"
      End If
    End Sub
    Mark
    -------------------

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    look at your previous post...
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  5. #5
    Fanatic Member steve65's Avatar
    Join Date
    Jun 2000
    Posts
    610

    Using API's

    Code:
    Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" _
        (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    
    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
    
    Public Const INVALID_HANDLE_VALUE = -1
    Public Const FILE_ATTRIBUTE_DIRECTORY = &H10
    
    Public 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
    
    Public 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
    This space for rent...

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