Solved

Checking if a directory exists

Posted on 1997-07-29
6
1,344 Views
Last Modified: 2013-11-20
(MFC,win32)
Whats the best way to check if a certain directory exists?
0
Comment
Question by:d3dman
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now