Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Testing File Existence

Testing File Existence

Scheduled Pinned Locked Moved C / C++ / MFC
sysadmintestingbeta-testingquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Srini Kella
    wrote on last edited by
    #1

    How could I test the existence of a file on a network drive ASynchronously? I used _access() function. But, that is a synchronous one; won't return immediately for invalid file paths. :confused:

    D A M 3 Replies Last reply
    0
    • S Srini Kella

      How could I test the existence of a file on a network drive ASynchronously? I used _access() function. But, that is a synchronous one; won't return immediately for invalid file paths. :confused:

      D Offline
      D Offline
      Doug Joseph
      wrote on last edited by
      #2

      Go back to old-school FILE *Stream; Stream = fopen( "filename", "r" ); if ( Stream == NULL ) { // That file or path doesn't exist } else { fclose( Stream ); }; Doug Joseph (Engineering Guy)

      S 1 Reply Last reply
      0
      • S Srini Kella

        How could I test the existence of a file on a network drive ASynchronously? I used _access() function. But, that is a synchronous one; won't return immediately for invalid file paths. :confused:

        A Offline
        A Offline
        Alvaro Mendez
        wrote on last edited by
        #3

        Here's some old code I have that does it:

        #include < sys/stat.h >

        // Returns true if the given szFile is a valid file.
        bool IsFile(const char* szFile)
        {
        assert(szFile);

        struct stat statBuffer;
        return (stat(szFile, &statBuffer) >= 0 &&		// make sure it exists
        	statBuffer.st\_mode & S\_IFREG);		// and it's not a directory
        

        }

        I'm not really sure what you mean by it needing to be asynchronous. I think what you need is a way to just check for existence and nothing else. Well, this should do it. Regards, Alvaro Insanity: doing the same thing over and over again and expecting different results. - Albert Einstein

        S 1 Reply Last reply
        0
        • D Doug Joseph

          Go back to old-school FILE *Stream; Stream = fopen( "filename", "r" ); if ( Stream == NULL ) { // That file or path doesn't exist } else { fclose( Stream ); }; Doug Joseph (Engineering Guy)

          S Offline
          S Offline
          Srini Kella
          wrote on last edited by
          #4

          Joe: Thanks for your response. But what I need is Asynchronous 'kind' of function to test a file existence. I have the following situation: Testing existence for \\Serv1\Vol1\Dir1\File1 is finished in few milli seconds if the server 'Serv1' is up and running. But, if somebody is shutting down the Serv1 and my program tests for the existence of a file on Serv1 at the same time, it takes huge time for the 'check existence' code to return. Under such situations, I wonder, if there is a kind of 'Asynchronous file existence checking' code which would just return immediately and fire a 'kind' of 'done' event later!!

          D D 2 Replies Last reply
          0
          • A Alvaro Mendez

            Here's some old code I have that does it:

            #include < sys/stat.h >

            // Returns true if the given szFile is a valid file.
            bool IsFile(const char* szFile)
            {
            assert(szFile);

            struct stat statBuffer;
            return (stat(szFile, &statBuffer) >= 0 &&		// make sure it exists
            	statBuffer.st\_mode & S\_IFREG);		// and it's not a directory
            

            }

            I'm not really sure what you mean by it needing to be asynchronous. I think what you need is a way to just check for existence and nothing else. Well, this should do it. Regards, Alvaro Insanity: doing the same thing over and over again and expecting different results. - Albert Einstein

            S Offline
            S Offline
            Srini Kella
            wrote on last edited by
            #5

            Alvaro, Thanks for your response. I have the following situation: Testing existence for \\Serv1\Vol1\Dir1\File1 is finished in few milli seconds if the server 'Serv1' is up and running. But, if somebody is shutting down the Serv1 and my program tests for the existence of a file on Serv1 at the same time, it takes huge time for the 'check existence' code to return. Under such situations, I wonder, if there is a kind of 'Asynchronous file existence checking' code which would just return immediately and fire a 'kind' of 'done' event later!!

            A 1 Reply Last reply
            0
            • S Srini Kella

              Alvaro, Thanks for your response. I have the following situation: Testing existence for \\Serv1\Vol1\Dir1\File1 is finished in few milli seconds if the server 'Serv1' is up and running. But, if somebody is shutting down the Serv1 and my program tests for the existence of a file on Serv1 at the same time, it takes huge time for the 'check existence' code to return. Under such situations, I wonder, if there is a kind of 'Asynchronous file existence checking' code which would just return immediately and fire a 'kind' of 'done' event later!!

              A Offline
              A Offline
              Alvaro Mendez
              wrote on last edited by
              #6

              Well, in that case then you need to spawn a separate thread to check the existence and somehow report back to the main thread what the result is. Regards, Alvaro Insanity: doing the same thing over and over again and expecting different results. - Albert Einstein

              1 Reply Last reply
              0
              • S Srini Kella

                How could I test the existence of a file on a network drive ASynchronously? I used _access() function. But, that is a synchronous one; won't return immediately for invalid file paths. :confused:

                M Offline
                M Offline
                Martin Ziacek
                wrote on last edited by
                #7

                I do not know, if it will work, but if you would try it and let us know, please: There is function FindFirstChangeNotification() to monitor changes within file system. If you could try to monitor all posible changes, then probably you might get some results as you require. Martin

                1 Reply Last reply
                0
                • S Srini Kella

                  Joe: Thanks for your response. But what I need is Asynchronous 'kind' of function to test a file existence. I have the following situation: Testing existence for \\Serv1\Vol1\Dir1\File1 is finished in few milli seconds if the server 'Serv1' is up and running. But, if somebody is shutting down the Serv1 and my program tests for the existence of a file on Serv1 at the same time, it takes huge time for the 'check existence' code to return. Under such situations, I wonder, if there is a kind of 'Asynchronous file existence checking' code which would just return immediately and fire a 'kind' of 'done' event later!!

                  D Offline
                  D Offline
                  Doug Joseph
                  wrote on last edited by
                  #8

                  Perhaps, you might want to try to ping the server, before checking for the file existance. Ping's should be realtively quick, especially if you set an artificial timeout on it. -Doug Doug Joseph (Engineering Guy)

                  1 Reply Last reply
                  0
                  • S Srini Kella

                    Joe: Thanks for your response. But what I need is Asynchronous 'kind' of function to test a file existence. I have the following situation: Testing existence for \\Serv1\Vol1\Dir1\File1 is finished in few milli seconds if the server 'Serv1' is up and running. But, if somebody is shutting down the Serv1 and my program tests for the existence of a file on Serv1 at the same time, it takes huge time for the 'check existence' code to return. Under such situations, I wonder, if there is a kind of 'Asynchronous file existence checking' code which would just return immediately and fire a 'kind' of 'done' event later!!

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Srini Kella wrote: ...I wonder, if there is a kind of 'Asynchronous file existence checking' code which would just return immediately and fire a 'kind' of 'done' event later!! This is a prime candidate for a worker (i.e., non-UI) thread.


                    A rich person is not the one who has the most, but the one that needs the least.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups