Solved

Checking if a directory exists

Posted on 1997-07-29
6
1,346 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Whole sheet autoscrub still needed 19 48
List out all word 7 254
has22 challenge 11 78
My project did see openJDK that I installed. What could be the problem 7 120
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

947 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

23 Experts available now in Live!

Get 1:1 Help Now