Testing File Existence
-
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:
-
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:
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)
-
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:
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
-
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)
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!!
-
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
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!!
-
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!!
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
-
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:
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
-
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!!
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)
-
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!!
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.