[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1377
  • Last Modified:

Checking if a directory exists

(MFC,win32)
Whats the best way to check if a certain directory exists?
0
d3dman
Asked:
d3dman
1 Solution
 
mbhaktaCommented:
Try using the FindFirstFile() , FindNextFile(), FindClose() calls to check the existance of the directory.

Make sure the WIN32_FIND_DATA.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY .
0
 
chensuCommented:
This way may be simpler.

CFileStatus status;
if (CFile::GetStatus(pszDir, status))   // static function
{
    BOOL bExist = status.m_attribute & CFile::directory;
}

0
 
mbhaktaCommented:
By using the CFile based technique you are stuck to MFC's overheads. Using Win32 SDK you have compiler independence. Another alternative is to use CFileFind which does what Win32's FindFirstFile does.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
davmarcCommented:
I guess d3dman is not going to use MFC just to check if a directory exist, so compiler independence is not really an issue IMO.
MFC overhead? Sure, bu on the other hand, the code gets easier. It's up to the reader go one way or the other.

Davide Marcato.
0
 
Amaresh080997Commented:
Dear d3dman,
     I can suggest a couple of ways to check if a particulkar directory exists.U can try them.
1) Try calling CFileFind::IsDirectory function.For syntax, kindly see the help.But this is an MFC function.
2) The next thing is , a sort of cheating and Shabby way of doing it.., but u can try.It is as follows:
First using "GetCurrentDirectory", get the current working directory.Then call "setCurrentDirectory()" by passing your directory under question as the parameter.Now, call "GetLastError()".If the return value of "GetLastError()" corresponds to
                a) 0L , it means, it is a directory.Then call "setcurrentdirectory" to change back to your original directory, that you got from calling "GetCurrentDirectory".
                b) Any other Number : Check with "..\MSDEV\WINERROR.h" file and by reading the corresponding error code,see if it is an invalid directory.
Kindly let me know if it works.
But do not forget to revert back to the old directory if the return value of "GetLastError" is zero.
Regards
Amaresh


0
 
Amaresh080997Commented:
Dear d3dman,
     I can suggest a couple of ways to check if a particulkar directory exists.U can try them.
1) Try calling CFileFind::IsDirectory function.For syntax, kindly see the help.But this is an MFC function.
2) The next thing is , a sort of cheating and Shabby way of doing it.., but u can try.It is as follows:
First using "GetCurrentDirectory", get the current working directory.Then call "setCurrentDirectory()" by passing your directory under question as the parameter.Now, call "GetLastError()".If the return value of "GetLastError()" corresponds to
                a) 0L , it means, it is a directory.Then call "setcurrentdirectory" to change back to your original directory, that you got from calling "GetCurrentDirectory".
                b) Any other Number : Check with "..\MSDEV\WINERROR.h" file and by reading the corresponding error code,see if it is an invalid directory.
Kindly let me know if it works.
But do not forget to revert back to the old directory if the return value of "GetLastError" is zero.
Regards
Amaresh


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now