Announcement

Collapse
No announcement yet.

How to check for empty directory in NT?

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

  • How to check for empty directory in NT?

    [Compiler PBDLL60]

    In Win95/98, if you want to check if a file exists, Power Basic uses DIR$(filemask). When an empty string is returned by this function, files matching the mask cannot be found and your code may generate a MessageBox at this point to inform the user.

    However, this trick does not work under Windows NT when the search directory is empty. Instead of displaying the application's own MessageBox, dr. Watson for Windows NT is launched, reporting "access violation".

    Does someone know how to avoid this, in other words: how to test for an empty dir under NT without launching that weird 'doctor'?
    Thanks and regards,

    Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
    http://zijlema.basicguru.eu
    *** Opinions expressed here are not necessarily untrue ***

  • #2
    NT has certain rules that must be followed, I have some code I tink that I can post that will solve that problem...


    That "weird doctor" is available in 95/98, it generates a log and shows a code segment where the access violation occured...

    You must also make sure that you do not have NTFS rights blocking your attempt to do whatever it is you are trying, in other words if you are a user trying to do a DIR on c:\winnt you may be seeing the good doctor frequently....

    I'll dig that code up and post it shortly (On my way out the door to work)..


    PS, starting thinking "FindFirstFile", think it works along those lines...

    Scott

    [This message has been edited by Scott Turchin (edited March 09, 2000).]
    Scott Turchin
    MCSE, MCP+I
    http://www.tngbbs.com
    ----------------------
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

    Comment


    • #3
      Code:
      Function Exist(ByVal filespec As String) Export As Long
        Local hDir     As Long
        Local FindData As WIN32_FIND_DATA
      
        FindData.dwFileAttributes = %FILE_ATTRIBUTE_DIRECTORY
        hDir = FindFirstFile(ByVal StrPtr(filespec), FindData)
        If hDir = %INVALID_HANDLE_VALUE Then
          Exit Function     'file not found
        End If
        Function = %TRUE
        FindClose hDir
      End Function

      ------------------
      Scott
      mailto:[email protected][email protected]</A>
      Scott Turchin
      MCSE, MCP+I
      http://www.tngbbs.com
      ----------------------
      True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

      Comment


      • #4
        Egbert --

        I'm apparently missing something, because I can't get DIR$ to fail on my NT machine, no matter what I try. Can you post the source code for a small program that fails?

        -- Eric

        ------------------
        Perfect Sync: Perfect Sync Development Tools
        Email: mailto:[email protected][email protected]</A>

        "Not my circus, not my monkeys."

        Comment


        • #5
          Ditto for me with NT5 (Win2K)... lets see the code... I suspect that the problem is elsewhere in your code.


          ------------------
          Lance
          PowerBASIC Support
          mailto:[email protected][email protected]</A>
          Lance
          mailto:[email protected]

          Comment


          • #6
            For Eric and Lance,

            The following code works perfectly under W95, but not under NT4.

            How to test it:
            Create a directory "C:\EMPTYDIR". Run the code. Under W95 it reports "No text files found" (even if the directory is not there it will work!!!!). Under NT, however, dr Watson is launched when the directory is not there or empty.

            After the creation of at least one file with the TXT-extension the code does work correctly on both platforms, reporting the number of files.

            Code:
            #COMPILE EXE
            #INCLUDE "WIN32API.INC"
            
            FUNCTION CountArticles(mask AS STRING) AS LONG
              LOCAL fd     AS WIN32_FIND_DATA
              LOCAL fh     AS LONG, counter AS LONG, yesno AS LONG
              LOCAL szText AS ASCIIZ * 255
            
              szText = mask + CHR$(0)
              fh = FindFirstFile(szText, fd)
              DO
                IF fd.dwFileAttributes = %FILE_ATTRIBUTE_ARCHIVE THEN INCR counter
                yesno = FindNextFile(fh, fd)
              LOOP UNTIL yesno = 0
              FindClose fh
              FUNCTION = counter
            END FUNCTION
            
            FUNCTION PBMAIN()
              LOCAL total AS LONG
              LOCAL mask AS STRING, directory AS STRING
            
              directory = "C:\EMPTYDIR\"
              mask = directory + "*.TXT"
            
              total = CountArticles(mask)
              IF total = 0 THEN
                MSGBOX "No text files found", 16, "Empty dir"
              ELSE
                MSGBOX TRIM$(STR$(total)) + " text file(s) found", 48, "Bingo"
              END IF
            END FUNCTION



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

            Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
            http://zijlema.basicguru.eu
            *** Opinions expressed here are not necessarily untrue ***

            Comment


            • #7
              NT5: works as it should be. But Chuck is right - no control.
              If fd = %INVALID_HANDLE_VALUE Then Good-buy

              [This message has been edited by Semen Matusovski (edited March 09, 2000).]

              Comment


              • #8
                Egbert, your code uses the "invalid" handle returned
                from FindFirstFile to try and get the next file. I am
                more surprised it succeeds under W95 than that it fails
                under WNT.

                Chuck

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

                Comment


                • #9
                  Egbert --

                  As Scott was the first to point out, you need to add this line...

                  Code:
                  fh = FindFirstFile(szText, fd)
                  [b]IF fh = %INVALID_HANDLE_VALUE THEN EXIT FUNCTION[/b]
                  DO
                  ...to avoid the GPF, because FindFirstFile returns that value (as documented) if it fails. If you pass that to FindNextFile... GPF.

                  In any event, I understood you to say that the DIR$ function was failing, not an API-based function...?

                  -- Eric

                  ------------------
                  Perfect Sync: Perfect Sync Development Tools
                  Email: mailto:[email protected][email protected]</A>



                  [This message has been edited by Eric Pearson (edited March 09, 2000).]
                  "Not my circus, not my monkeys."

                  Comment


                  • #10
                    Hi guys,

                    First of all, I want to apologize for the false trail. My app (the real one, not the dummy source on this page), that I developed a few weeks ago under W95 did always work on that platform, with or without file presence. Yesterday I 'installed' it on NT for the first time and 'oops'. I did'nt remember that I had used an API routine instead of Power Basic's DIR$ and, to be honest, I was too busy to take a look in the source. Sorry.

                    Nevertheless, after posting my piece of code you bailed me out very soon. Indeed, you may wonder why it worked on W95 so well. The NT-crash was, in fact, the normal punishment for this bad piece of code. Thanks for your help.

                    Regards,
                    Egbert (a basic guru?)

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


                    [This message has been edited by Egbert Zijlema (edited March 09, 2000).]

                    Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
                    http://zijlema.basicguru.eu
                    *** Opinions expressed here are not necessarily untrue ***

                    Comment

                    Working...
                    X