[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Checking if a directory exists

Posted on 1997-07-29
6
Medium Priority
?
1,375 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 100 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Suggested Courses

656 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