Announcement

Collapse
No announcement yet.

File Exists fn

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

  • File Exists fn

    Hello, folks -

    I've had to pick up an old project that I wrote a long time ago.
    The app is running under MS-DOS 6.22 and I'm writing the code to compile under PD for DOS, version 3.22.

    One of the functions that I wrote is called FileExists. It is to return %False if it cannot fine the file that was passed to it as an argument, and to return %True if it finds the file.
    It also attempts to cope with the entire path information provided as an argument, for those situations where the sought file is expected in a subdirectory other than what is currently local.

    For some reason, the program does not detect files that it should.

    Do any of you have a similar function that I could study?

    Kurt Schultz
    Don't sweat it, it's only ones and zeros

  • #2
    Bound to be one in the PB/DOS examples folder...

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

    Comment


    • #3
      Code:
      FUNCTION Exists (s AS STRING) AS INTEGER
        FUNCTION = DIR$(S) <> ""
      END FUNCTION
      Michael Mattias
      Tal Systems Inc. (retired)
      Racine WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Hi, Tom -

        If so (and it is likely), it would be a simplified version that's not sophisticated enough for what needs to be done in this app.
        One of the problems I encountered was in trying to detect files that existed in directories other than the currently-local directory.


        Hi, Michael -

        If I remember correctly, your approach has problems if it gets a null string as an argument.
        This is essentially what my routine does after storing and removing the path from the file name, except that the routine also tests for null input.
        If the input is not null, then it tests via the DIR$.

        Kurt Schultz

        ------------------
        Don't sweat it, it's only ones and zeros

        Comment


        • #5
          If I'm reading your problem right, the following example should
          be useful.
          Code:
          path$ = "c:\mystuff\pb35\"
          fname$ = "somefile.bas"
          te$ = dir$(path$ + fname$,{Attribute))
          If te$ <> "" then te$ will only contain the file name being
          sought. DIR$ will return only the file name whether you are in
          the directory in question or not.

          Also, PB/DOS does not support LFN's. It can only digest SFN's.

          You might want to check out the ATTRIBUTE option. That will
          reduce the number of files returned.


          ------------------
          There are no atheists in a fox hole or the morning of a math test.
          If my flag offends you, I'll help you pack.

          Comment


          • #6
            Hi, Mel -

            My approach is somewhat inverted compared to yours.

            I strip off the drive and directory information, leaving only the filename, then switch to that directory, trigger the DIR$ command, then restore to the directory that was in use before the function was called.

            Specifying an attribute code would be redundant.

            It seems like I'm getting a null string from the DIR$ fn even if the file exists.

            I'm tempted to bypass the DIR$ fn and write it as a DOS call.


            Kurt Schultz


            ------------------
            Don't sweat it, it's only ones and zeros

            Comment


            • #7
              A lot of old MS-DOS BASIC code was originally created before all BASICs had functions like DIR$, or before those BASIC's handled a PRINT statement via direct screen writes, or before there was automatic string garbage collection, or when machines were like my first PC, a screaming dual-speed 4.77/6.0 Mhz.

              Some of those library- and utility-type functions in commercial libraries just do not make sense anymore, as "what they replaced" or "what gap they filled" no longer need replacement or remain gaps.

              MCM
              Michael Mattias
              Tal Systems Inc. (retired)
              Racine WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                Why would you assume there would be a problem handling drives or
                directories? There is no reason for such a limitation.

                See the Exist function in your PB/DOS Examples folder, DOSUNIT.BAS.

                Yes, you may need to filter out empty filespecs yourself, if you
                expect to pass them.

                If you are using LFN filenames (Windows "Long FileName" convention),
                you will not be able to pass those directly. Either use the short
                version of the name, or try one of the LFN libraries in the Download
                section.

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

                Comment


                • #9
                  Hi, Tom -

                  The assumption about drives and directories is related to a User Configuration issue, not directed at any supposed limitation in the compiler.

                  Also, the app needs to interface with several other companies' programs, and they hand off the parameters to my program in a variety of ways. Reduction to Drive$ + Directory$ + FileName$ seemed most reasonable for handling all of the potential situations.

                  LFNs are not at issue, this legacy software predates Win95. Come to think of it, I believe this project was started under PB 2.2 (or maybe some earlier version).

                  I looked at the DOSUNIT and DIRUNIT stuff earlier today, but had an appointment for some new glasses and couldn't play with the code as much as I wanted to.

                  I'm tempted to write a DTA monitor to see if things are happening as I think they ought to be happening.

                  I'll do some more on it tomorrow.


                  Kurt Schultz


                  ------------------


                  [This message has been edited by Kurt Schultz (edited June 18, 2003).]
                  Don't sweat it, it's only ones and zeros

                  Comment


                  • #10
                    Here's the code:

                    FUNCTION FileExists (FileExistsFileSpec AS STRING) LOCAL PRIVATE
                    ' If the FileExistsFileSpec includes path information, it will be used,
                    ' otherwise, this FUNCTION will search in the current default
                    ' directory. If you include a wild card in the FileExistsFileSpec, any
                    ' matching file name will make this FUNCTION return a logical
                    ' 'True' (non-zero) value.

                    DIM DirName AS STRING
                    DIM OldPath AS STRING
                    DIM OldParam AS STRING
                    DIM PathPart AS STRING

                    OldParam = FileExistsFileSpec
                    DirName = ""
                    OldPath = ""

                    ' erosive transfer of path info
                    WHILE INSTR(FileExistsFileSpec, "\")
                    ' while FileExistsFileSpec contains "\" character(s)
                    ' build DirName$
                    PathPart = EXTRACT$(FileExistsFileSpec, "\") + "\"
                    DirName = DirName + PathPart
                    FileExistsFileSpec = LTRIM$(FileExistsFileSpec, PathPart)
                    FileExistsFileSpec = LTRIM$(FileExistsFileSpec, "\")
                    WEND

                    IF LEN(DirName) THEN
                    OldPath = CURDIR$
                    CHDIR RTRIM$(DirName, "\")
                    END IF

                    IF FileExistsFileSpec = "" THEN
                    ' Error - no filename passed to procedure
                    FileExists = %False
                    ELSE
                    IF DIR$(FileExistsFileSpec) = "" THEN
                    ' Error - File not found
                    FileExists = %False
                    ELSE
                    ' Success - File Found
                    FileExists = %True
                    END IF
                    END IF

                    IF LEN(OldPath) > 0 THEN CHDIR RTRIM$(OldPath, "\")

                    FileExistsFileSpec = OldParam

                    END FUNCTION

                    It's not finding a file that is there.

                    Kurt Schultz

                    Yuck, it's trashed my format... ok, hope you can read it
                    correctly - KAS
                    ------------------


                    [This message has been edited by Kurt Schultz (edited June 19, 2003).]
                    Don't sweat it, it's only ones and zeros

                    Comment


                    • #11
                      Kurt,

                      Are you familiar with using code written for MASM 6.14 that is
                      compiled to OBJ files for inclusion in main PB/DOS EXE's? If
                      you are, I could post my "ExistFile" source code. It autodetects
                      whether the current environment will support LFN's or not, and then
                      uses the LFN or the SFN approach, as appropriate. Either that, or
                      I could boil the OBJ file down to just that function, and e-mail the
                      compiled file to you.


                      ------------------

                      Comment


                      • #12
                        Hi, Clay -

                        I can read ASM, although not really well. I almost hate to admit it, but at one point I was actually better with machine code than with ASM. That was before I retired my 8080A machine.

                        It would be a little tedious, but I could write some inline ASM that would trigger the appropriate DOS interrupt for this situation. I'd probably crib most of it from the Programmer's Problem Solver.

                        Once again, LFNs are not an issue; the app runs under MS-DOS 6.22 and all subordinate files conform to the 8.3 filename.typ limits.


                        Kurt Schultz


                        ------------------
                        Don't sweat it, it's only ones and zeros

                        Comment


                        • #13
                          Kurt,

                          The detection of whether or not the current environment supports
                          LFN's is automatic in my OBJ file's function - there is no need
                          to alter anything in the code, as, since you say your proggies
                          will run under plain DOS, then it will detect that, and automatically
                          use the SFN part of its code. So, your answer was not very, ummm,
                          informative - would you like for me to post the source code, or
                          e-mail to you the compiled OBJ file? I love dabbling in ASM, so if
                          you want, I'll even port the code over to a form that can be compiled
                          as a PB/DOS UNIT. 'Twould be quite easy to do.


                          ------------------

                          Comment


                          • #14
                            Hi, Clay -

                            If the ASM source code isn't really long, I suggest you post it in here.

                            BTW, I appreciate your interest in this matter & your willingness to help.


                            Kurt Schultz


                            ------------------
                            Don't sweat it, it's only ones and zeros

                            Comment


                            • #15
                              Kurt,

                              The pleasure is mine.

                              OK, but before I post the code, I am going to convert it to
                              PB/DOS UNIT format. That way, any "lurkers" will not have to have
                              the MASM 6.14 compiler handy to use it - I'll post it in such a
                              way that all anybody has to do is copy-and-paste it, then compile it.
                              So, it'll take me a while (couple of hours?), as the OBJ file's
                              source file actually contains a number of different routines, so
                              I will have to sort it out. And, that's the other reason for converting
                              it to UNIT form, so I do not post a big bit of spaghetti code. <grin>


                              ------------------

                              Comment


                              • #16
                                Hi, Clay -

                                If it's "spaghetti code", couldn't you just "Cut-n-PASTA" it?

                                (sorry, couldn't resist)


                                Kurt Schultz


                                ------------------
                                Don't sweat it, it's only ones and zeros

                                Comment


                                • #17
                                  #IF 0
                                  Kurt,

                                  <laugh> Good one! <chuckle>

                                  OK, here's the code:
                                  #ENDIF

                                  Code:
                                  $COMPILE UNIT
                                  $DIM ALL
                                  
                                  TYPE DTA
                                      junk1(1 TO 21) AS BYTE
                                      nAttributes AS BYTE
                                      junk2 AS DWORD
                                      nFileSize AS DWORD
                                      nFileName AS ASCIIZ*13
                                  END TYPE
                                  
                                  FUNCTION Test (BYVAL A AS STRING, BYVAL B AS INTEGER) PUBLIC AS INTEGER
                                      DIM wx AS ASCIIZ*68
                                      DIM tempdta AS DTA
                                      wx = A
                                      ! lea dx, tempdta
                                      ! mov ah, &H1A
                                      ! int &H21
                                      ! xor ch, ch
                                      ! mov cl, &B00100111
                                      ! lea dx, wx
                                      ! mov ax, &H4E00
                                      ! stc
                                      ! int &H21
                                      ! jnc P1A
                                      ! xor ax, ax
                                      ! jmp P2A
                                      P1A:
                                      ! mov ax, 1
                                      P2A:
                                      ! mov bx, B
                                      ! or bx, bx
                                      ! jz P3A
                                      ! xor ax, 1
                                      P3A:
                                      ! neg ax
                                      ! mov FUNCTION, ax
                                      ! push ss
                                      ! pop ds
                                  END FUNCTION
                                  #IF 0
                                  Argument 'A' is the path/name of the file whose existence you are
                                  checking for. Argument 'B' tells the function to reverse its
                                  return. If the file does exist, and 'B' is zero, then it returns -1.
                                  If the file does not exist, and 'B' is zero, it returns 0 (zero).
                                  If the file does exist, and 'B' is nonzero, it returns 0. If the file
                                  does not exist, and 'B' is nonzero, it returns -1.

                                  Any questions, etc., feel free to ask!

                                  P.S. This function only includes code that works for 8.3 file names - it
                                  will NOT work with LFN's.
                                  #ENDIF




                                  ------------------

                                  Comment


                                  • #18
                                    Kurt,

                                    This is what I use to determine if a file exist:


                                    FILE$ = "F:\public\FileName.Dat" 'Path and File name here
                                    x$ = DIR$(file$) 'Look for File

                                    IF x$ = RIGHT$(file$,12) THEN 'Check just the name for match
                                    Indicator = 1 'True
                                    ELSE
                                    Indicator = 0 'False
                                    END IF



                                    Hope this helps.


                                    ------------------
                                    Michael Burgett
                                    Michael Burgett

                                    Comment


                                    • #19
                                      Hi, Mike -

                                      Your name's familiar - FIDO/BBS operator? I think you used to be the
                                      SYSOP on one of the boards I used.

                                      If you're the same guy, then Gene, Lance and Ray would be curious to
                                      know if you're ok & how you're doing.

                                      Regarding the FileExists fn, I've reverted to the simpler way of checking
                                      (because it works), so that I can resume testing the remainder of the
                                      package. I know there was a reason that I went to the more complex fn,
                                      but it's been so long that I cannot remember what the issue was.

                                      I guess if I get back into the mindset I had then, I'll be able to
                                      resume work on it.


                                      Kurt Schultz


                                      ------------------


                                      [This message has been edited by Kurt Schultz (edited June 22, 2003).]
                                      Don't sweat it, it's only ones and zeros

                                      Comment


                                      • #20
                                        Hi, Lance -

                                        If you're able to, I'd like for you to try the source code I put
                                        in this thread. I'm curious to know if it fails for you the
                                        same way it fails for me.


                                        Kurt Schultz


                                        ------------------
                                        Don't sweat it, it's only ones and zeros

                                        Comment

                                        Working...
                                        X