Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Small routine to detect if folder exists

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PBWin Small routine to detect if folder exists

    #COMPILER PBWIN 9
    #DIM ALL
    #INCLUDE "Win32API.inc"

    ' ------------------------------------------------------
    ' Small routine to detect if folder exists
    ' Pequeña rutina para detectar si folder existe
    ' ------------------------------------------------------

    '========================================================
    FUNCTION isFolder(BYVAL Path_Folder AS STRING) AS LONG
    ' Folder exists = -1 noexists = 0
    LOCAL FileData AS DIRDATA, sResult AS STRING
    sResult = DIR$(Path_Folder, %SUBDIR, TO FileData)
    IF sResult<>"" THEN
    FUNCTION = -1
    ELSE
    FUNCTION= 0
    END IF
    END FUNCTION

    '========================================================

    FUNCTION PBMAIN () AS LONG
    LOCAL sFoldername$
    sFoldername = "C:\PBWIN80\" '<---change
    IF isFolder (sFoldername) THEN
    MSGBOX sFoldername + " <-exists!", %MB_OK+%MB_ICONINFORMATION, " Folder Test "
    ELSE
    MSGBOX sFoldername + " does not exist!", %MB_OK+%MB_ICONINFORMATION, " Folder Test "
    END IF
    END FUNCTION

  • #2
    Note:
    A hidden folder only exists if at least one visible object is contained.

    Comment


    • #3
      In my view, following code is better:

      Code:
      #COMPILER PBWIN 9
      #DIM ALL
      
      FUNCTION isFolder(BYVAL Path_Folder AS STRING) AS LONG
      ' Folder exists = -1 noexists = 0
      [B]LOCAL lResult AS LONG
          lResult = GETATTR(Path_Folder)
          IF (lResult AND %SUBDIR) = %SUBDIR THEN[/B]
              FUNCTION = -1
          ELSE
              FUNCTION= 0
          END IF
      END FUNCTION
      
      FUNCTION PBMAIN () AS LONG
      LOCAL sFoldername$
          sFoldername = "C:\PBWIN64" '<---change
          IF isFolder (sFoldername) THEN
              MSGBOX sFoldername + " <-exists!", %MB_OK+%MB_ICONINFORMATION, " Folder Test "
          ELSE
              MSGBOX sFoldername + " does not exist!", %MB_OK+%MB_ICONINFORMATION, " Folder Test "
          END IF
      END FUNCTION

      Comment


      • #4
        These forums are excellent because they give the opportunity to contribute and learn

        An example is worth a thousand words
        Un ejemplo vale mas que mil palabras

        Thanks
        Bernhard Fomm

        Comment


        • #5
          more better

          Code:
          FUNCTION isFolder (BYVAL p AS STRING) AS LONG
              FUNCTION = (GETATTR(p) AND &B010000) = &B010000
          END FUNCTION

          Comment


          • #6
            more better
            Code:
            FUNCTION isFolder (BYVAL p AS STRING) AS LONG
                FUNCTION = (GETATTR(p) AND &B010000) = &B010000
            END FUNCTION
            IMO using inline numeric literals - "magic numbers" such as "&b010000" - is NEVER better.
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Another way to determine if Folder Exists or if FileExist as well.

              Will Prompt with Message box with error reason or Return True if it exists, False if it does not
              Better ways to return the error than what is shown

              If searching folder ensure path ends in "\" backslash if not it assumes you are looking for a file


              Code:
              FUNCTION isFolder(BYVAL Path_Folder AS ASCIIZ * %Max_Path) AS LONG
                    LOCAL hsearch AS DWORD , WFD AS WIN32_FIND_DATA, strBuffer AS ASCIIZ * 255, eCode AS LONG
              'Look up Folder (could be file as well if desired)
                    hSearch = FindFirstFile(Path_Folder, WFD)
              'did we error
                    IF hsearch = %INVALID_HANDLE_VALUE THEN
                       eCode = GetLastError
                       FormatMessage %FORMAT_MESSAGE_FROM_SYSTEM, BYVAL %NULL, ecode, %NULL, strBuffer, SIZEOF(strBuffer), BYVAL %NULL
                       MSGBOX "isFolder Returned Error: " & FORMAT$(eCode, "#### ") & strBuffer
                       FUNCTION = %FALSE
                    ELSE
                       FUNCTION = %TRUE
                    END IF
              END FUNCTION
              Last edited by Ronald Robinson; 4 Dec 2009, 02:07 PM. Reason: Changed to show System Error code on %INVALID_HANDLE_VALUE

              Comment


              • #8
                This function generates error 76 if the folder does not exist

                Code:
                FUNCTION isFolder (BYVAL p AS STRING) AS LONG
                    FUNCTION = (GETATTR(p) AND &B010000) = &B010000
                END FUNCTION


                This is what I use

                Code:
                FUNCTION Directory_Exists( BYVAL DirPath AS STRING ) AS LONG
                	'
                	LOCAL TS1 AS STRING
                	'
                	TRY		' <o><o><o><o> TRY ERROR TRAP <o><o><o><o>
                		'
                		TS1 = CURDIR$
                		CHDIR DirPath
                		'
                		CATCH		' <o><o><o><o> CATCH ERROR TRAP <o><o><o><o>
                		'
                		FUNCTION = ERR
                		exit function
                		'
                		FINALLY		' <o><o><o><o> FINALLY ERROR TRAP <o><o><o><o>
                		'
                	END TRY		' <o><o><o><o> TRY END ERROR TRAP <o><o><o><o>
                	'
                	CHDIR TS1
                	'
                END FUNCTION
                Last edited by Steve Bouffe; 6 Jan 2010, 03:16 AM.

                Comment


                • #9
                  Code:
                  Declare Function PathIsDirectory Lib "SHLWAPI.DLL" Alias "PathIsDirectoryA" (pszPath  As  AsciiZ) As Long
                  Return Value

                  Returns TRUE if the path is a valid directory (FILE_ATTRIBUTE_DIRECTORY is set), or FALSE otherwise.
                  Arie Verheul

                  Comment


                  • #10
                    Neat

                    Just did some testing and the function doesn't return %TRUE but does return %FALSE if the directory does not exist.
                    Last edited by Steve Bouffe; 6 Jan 2010, 06:48 AM.

                    Comment


                    • #11
                      This old technique works all the time

                      Code:
                      TRY
                        OPEN path$+"NULL" FOR INPUT AS #1: CLOSE #1
                        'Directory exist
                      CATCH
                        'Directory does not exist
                      END TRY
                      Last edited by Guy Dombrowski; 6 Jan 2010, 07:39 AM.
                      Old QB45 Programmer

                      Comment


                      • #12
                        the [PathIsDirectory] function doesn't return %TRUE but does return %FALSE if the directory does not exist
                        Formally speaking you are right Steve. For a regular subdirectory the PathIsDirectory function
                        returns the value of 16, whilst %True is ususally considered to be +/- 1.

                        Arie Verheul

                        Comment


                        • #13
                          Arie, false is always 0, true is any value other than false.
                          Jeff Blakeney

                          Comment


                          • #14
                            In PB 9.03 / CC 5.03 ISFOLDER() is available!

                            Comment

                            Working...
                            X