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

                Working...
                X