CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 1999
    Location
    California, USA
    Posts
    40

    How to detect if a directory already exists using "If" statement

    How do i detect if a folder(directory) already exists using the if statement?

    PanasonicSubz

  2. #2

    Re: How to detect if a directory already exists using "If" statement


  3. #3
    Join Date
    Nov 1999
    Posts
    1

    Re: How to detect if a directory already exists using "If" statement

    ry an error handler on the chdir command:


    Sub ChangePath( sPath as string )
    on error GoTo errDir

    ChDir (sPath)

    Exit Sub

    errDir:

    If Err.Number = 76 then
    MsgBox "Yep, path " + sPath + " doesn't exist"
    else
    MsgBox Err.Description, vbOKOnly, Err.Number
    End If

    End Sub






  4. #4
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: How to detect if a directory already exists using "If" statement

    Place a reference in your project to Microsoft Scripting Runtime (sscrun.dll) and use the following code :



    Dim oFSO as FileSystemObject

    set oFSO = new FileSystemObject

    If oFSO.FolderExists("c:\temp\whatever") then
    '
    else
    '
    End If




    It's probably the easiest method available, and you get a whole bunch of other functionality from using the Scripting Objects.



    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

  5. #5
    Join Date
    Nov 1999
    Location
    Trinidad, West Indies
    Posts
    10

    Re: How to detect if a directory already exists using "If" statement

    Try using the Dir function
    Dir[(pathname[, attributes])]


    'Variable declaration
    Dim pathname as string

    'Variable initialization
    pathname = "Type/Retrieve directory name"

    'Test existence
    If (Dir(pathname, vbDirectory) <> "") then
    'Directory Exists
    'Dir fctn returns directory name
    else
    'Directory does not exist
    'Dir fctn returns empty string
    End If





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

Featured