Solved

Checking if a directory exists

Posted on 1997-07-29
6
1,365 Views
Last Modified: 2013-11-20
(MFC,win32)
Whats the best way to check if a certain directory exists?
0
Comment
Question by:d3dman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 4

Accepted Solution

by:
mbhakta earned 50 total points
ID: 1302901
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
 
LVL 23

Expert Comment

by:chensu
ID: 1302902
This way may be simpler.

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

0
 
LVL 4

Expert Comment

by:mbhakta
ID: 1302903
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!

 
LVL 4

Expert Comment

by:davmarc
ID: 1302904
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
 

Expert Comment

by:Amaresh080997
ID: 1302905
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
 

Expert Comment

by:Amaresh080997
ID: 1302906
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question