Announcement

Collapse
No announcement yet.

better way to check if file exists?

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

  • better way to check if file exists?

    Hello everyone!


    I need some advice as to how I can check if a file exists WITHOUT using DIR$ or (OPEN,check LOF = 0,CLOSE,KILL).

    ------------------
    Cheers

  • #2
    GetAttr

    ------------------
    E-MAIL: [email protected]

    Comment


    • #3
      Thank you Semen!

      It works great.

      ------------------
      Cheers

      Comment


      • #4
        Code:
        Function FSO_FileExists(ByVal FileSpec$)Export As Long
        Local fd As WIN32_FIND_DATA
        Local fAttr As Dword
        Local hFind&
          If Len(FileSpec$)= 0 Then Function = %false:Exit Function
          hFind& = FindFirstFile(ByVal StrPtr(FileSpec$), fd)
          If hFind& = %INVALID_HANDLE_VALUE Then Function = %False:Exit Function
          Call FindClose(hFind&)
          fAttr = fd.dwFileAttributes
          Function = %True
          'NotDirectory|NotTemporary
          If (Bit(fAttr, 4)=1) Or (Bit(fAttr,8)=1) Then Function = %false
        End Function
        ------------------
        Fred
        mailto:[email protected][email protected]</A>
        http://www.oxenby.se

        Fred
        mailto:[email protected][email protected]</A>
        http://www.oxenby.se

        Comment


        • #5
          Thank you Fred!


          Maybe PowerBasic would like to add EXISTS as a feature to the compiler? I think every one of us has run into this at least once!

          Code:
          if EXISTS(File) then
              open File for binary as #1
          end if
          ------------------
          Cheers

          Comment


          • #6
            There is an EXISTS() function in COMMON32.BAS using DIR$(). It is not quite as comprehensive as Fred's though, but is equally useful since it uses just a single line of code.
            Code:
            FUNCTION Exist(file AS STRING) EXPORT AS INTEGER
              FUNCTION = LEN( DIR$(file, 17) ) > 0
            END FUNCTION
            ------------------
            Lance
            PowerBASIC Support
            mailto:[email protected][email protected]</A>
            Lance
            mailto:[email protected]

            Comment


            • #7
              The problem with using DIR$(filespec) in an EXIST function is,
              it throws off DIR$ (without filespec) continuity in the rest of your
              app.

              We do have an "exists" function on the wish list. I'll add a tick.

              ------------------
              Tom Hanlin
              PowerBASIC Staff

              Comment


              • #8
                Thank Tom!

                Thats persactly why I needed this...

                ------------------
                Cheers

                Comment


                • #9
                  Originally posted by Lance Edmonds View Post
                  There is an EXISTS() function in COMMON32.BAS using DIR$(). It is not quite as comprehensive as Fred's though, but is equally useful since it uses just a single line of code.
                  Code:
                  FUNCTION Exist(file AS STRING) EXPORT AS INTEGER
                  FUNCTION = LEN( DIR$(file, 17) ) > 0
                  END FUNCTION
                  ------------------
                  Lance
                  PowerBASIC Support
                  mailto:[email protected][email protected]</A>
                  I have tried this in the latest version of PB/CC and get the following error when trying to compile my cpde:

                  Code:
                  Error 480 in C:\Documents and Settings\Administrator\My Documents\hunter.bas(42:030): Parameter mismatches definition
                  Line 42: IF Exist(Listing(idx)) THEN
                  Basically what I am trying to do is compare a list of files loaded from a text file to a directory of files on disk to make sure they exist.

                  Comment


                  • #10
                    ISFILE function
                    The world is full of apathy, but who cares?

                    Comment


                    • #11
                      Yep, this is a 20 year old thread and any suggestions are obsolete..

                      The last couple of versions of PB compilers have built in ISFILE() and ISFOLDER() functions

                      Comment


                      • #12
                        Code:
                        TESTD = PathIsDirectoryA(BYCOPY TMPNAME)
                        ? STR$(TESTD) + " ASC"
                        
                        IF TESTD = 16 OR TESTD = 8208 THEN
                        '
                        ELSE
                        
                        
                        ? " This IS not A Folder _ Directory "
                        END IF
                        
                        
                        TESTE = PathIsDirectoryEmptyA(BYCOPY TMPNAME)
                        
                        If TESTE = 0 OR TESTE = 8224 Then
                        ? " Folder _ Directory is NOT Empty "
                        EXIT Function
                        ELSE
                        ' RmDir TmpName
                        '?"EMPTY"
                        
                        End If
                        
                        Close
                        PathIsDirectoryA return if directory

                        PathIsDirectoryEmptyA return if directory is empty

                        enjoy


                        Comment


                        • #13
                          What's the significance of 16, 8208 and 8224? Are they just return values you got for specific tests?
                          What happens if your file/folder names are Unicode?
                          '
                          Code:
                          #COMPILE EXE
                          #DIM ALL
                          
                          FUNCTION PBMAIN() AS LONG
                           LOCAL wsName AS WSTRING
                           wsName = "ABE"
                           ? IsFileOrFolder(wsName)
                          END FUNCTION
                          
                          FUNCTION IsFileOrFolder(s AS WSTRING) AS WSTRING
                              LOCAL lresult AS LONG
                              IF ISFILE(s) THEN
                                  lresult = 1 'file
                              ELSE
                                  IF ISFOLDER(s) THEN
                                      IF DIR$(s & "\*.*",%HIDDEN OR %SYSTEM OR %SUBDIR) = "" THEN
                                          lresult = 2 ' empty folder
                                      ELSE
                                          IF DIR$(s & "\*.*",%HIDDEN OR %SYSTEM) = "" THEN
                                               lresult = 3 ' folder, has subfolder but no files
                                          ELSE
                                              lresult = 4 ' folder has  files
                                          END IF
                                      END IF
                                  END IF
                              END IF
                          FUNCTION = s & " is " &  CHOOSE$(lResult," a File","an Empty Folder","a Folder with sub folders but no files","a Folder containing files" ELSE "Neither a file nor a folder")
                          END FUNCTION
                          '

                          Comment


                          • #14

                            FILE_ATTRIBUTE_DIRECTORY = 16 (bit 4)
                            FILE_ATTRIBUTE_ARCHIVE = 32 (bit 5)
                            FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192 (bit 13)

                            16 OR 8192 = 8208
                            32 OR 8192 = 8224

                            An AND with his result variables from API calls would have given TRUE or FALSE answer
                            Dale

                            Comment


                            • #15
                              Originally posted by Dale Yarker View Post
                              FILE_ATTRIBUTE_DIRECTORY = 16 (bit 4)
                              FILE_ATTRIBUTE_ARCHIVE = 32 (bit 5)
                              FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192 (bit 13)

                              16 OR 8192 = 8208
                              32 OR 8192 = 8224

                              An AND with his result variables from API calls would have given TRUE or FALSE answer
                              So he'd get the wrong result if the folder was not archived or was indexed or had any other attributes set (Hidden, System...) ?

                              Both of the API functions he is calling just return a BOOLEAN according to MS:
                              https://docs.microsoft.com/en-us/win...thisdirectorya
                              Return value
                              Type: BOOL
                              Returns (BOOL)FILE_ATTRIBUTE_DIRECTORY if the path is a valid directory; otherwise, FALSE.

                              https://docs.microsoft.com/en-us/win...irectoryemptya
                              Return value
                              Type: BOOL
                              Returns TRUE if pszPath is an empty directory. Returns FALSE if pszPath is not a directory, or if it contains at least one file other than "." or "..".

                              Comment


                              • #16
                                So he'd get the wrong result if the folder was not archived or was indexed or had any other attributes set (Hidden, System...) ?
                                Yeap.

                                Added to the links you gave:
                                File Attribute Constants (WinNT.h) - Win32 apps | Microsoft Docs
                                Dale

                                Comment


                                • #17
                                  Originally posted by Dale Yarker View Post
                                  "For a list of related APIs and topics, see the See Also section."
                                  No mention of PathIsDirectory or PathIsDirectoryEmpty in that section.
                                  Would you rely on them returing a full FileAttribute mask value?

                                  I'd stick with a boolean (IsTrue or IsFalse or =0 or <>0) test if using those functions,

                                  Comment


                                  • #18
                                    Would you rely on them returing a full FileAttribute mask value?
                                    No, GETATTR.

                                    What did OP had against DIR$ ?? Just ISFILE now! (mentioned above, if you go through old and really posts above.
                                    Dale

                                    Comment


                                    • #19
                                      Originally posted by Dale Yarker View Post
                                      What did OP had against DIR$ ??
                                      Posts 7 and 8.
                                      "The problem with using DIR$(filespec) in an EXIST function is,it throws off DIR$ (without filespec) continuity in the rest of your app."
                                      and
                                      "Thats persactly why I needed this..."

                                      Which could also be a problem in my Post #13
                                      But straighforward use of ISFILE and ISFOLDER is not a problem.

                                      Comment


                                      • #20
                                        They didn't heed post 2.
                                        Dale

                                        Comment

                                        Working...
                                        X