Solved

Checking directory existance

Posted on 1998-08-28
16
420 Views
Last Modified: 2008-02-01
I want to check to see if a directory exists before using it in code.  What functions/methods are available to check a string which contains a path specification for existance?
0
Comment
Question by:cwirrgan
  • 7
  • 4
  • 3
  • +1
16 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1171481
this is platform dependant.  What OS are we taling about?
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1171482
It looks like the function access (or _access or __access etc...) will do what you want, but as nietod says, this is platform dependent.
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1171483
If the "access" function is available, try something like the following:

if (access (PathToDirectory, 0) == 0)
{ // Do what you want if it exists
}
else
{ // Do what you want if it doesn't
}
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1171484
According to the Visual C++ documentation, the access function is ANSI compliant, so it should work with most everything (Windows NT, 95, and 98 for sure).
0
 
LVL 2

Accepted Solution

by:
milenvk earned 20 total points
ID: 1171485
If you are programming for win32 here is my own function that checks for file and directory existance:

BOOL CCMSProIEStart::FileExist(LPCSTR fileName)
{
  WIN32_FIND_DATA ffd;
  HANDLE hFF = NULL;
  if((hFF = ::FindFirstFile(fileName, &ffd)) == INVALID_HANDLE_VALUE)
    return FALSE;
  ::FindClose(hFF);
  return TRUE;
}

0
 
LVL 22

Expert Comment

by:nietod
ID: 1171486
According to my documentation the functon is not a standard function, it is provided by Microsoft for windows platforms, so it won't be availalbe in non-windows programs.
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1171487
From MS documentation:

"The Microsoft run-time library supports American National Standards Institute (ANSI) C and UNIX® C. In this book, references to UNIX include XENIX®, other UNIX-like systems, and the POSIX subsystem in Windows NT and Windows 95. The description of each run-time library routine in this book includes a compatibility section for these targets: ANSI, Windows 95 (listed as Win 95), and Windows NT (Win NT). All run-time library routines included with this product are compatible with the Win32 API."

To me that means it is Ansi complient since the function is part of the run time library.
0
 

Author Comment

by:cwirrgan
ID: 1171488
The platfom is Windows NT/95
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 6

Expert Comment

by:thresher_shark
ID: 1171489
milenvk - Isn't that a little overkill considering what I proposed?
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1171490
cwirrgan - Will access work in that case?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1171491
thresher_shark, you have it backwards, it says

"All run-time library routines included with this product are compatible with the Win32 API"

that means that you can use any run-time library procedure from within windows.  It doesn't mean that you can use any non-standard procedure provided by Microsoft on another (non-microsoft) platform.  The access() procedure is not part of the standard run-time library.  Other libraries or platforms may not (probably will not) have it.
0
 

Author Comment

by:cwirrgan
ID: 1171492
Access will work in my case.
0
 

Author Comment

by:cwirrgan
ID: 1171493
Access will work in my case.
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1171494
Oh, yes you are right nietod.  Thank you for clarifying.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1171495
If you are going to use access(), the points really should have gone to thresher_shark, not milenvk.  If you want to use the window API directly, then the GetFileAttributes() function could be used instead of the functions that milenvk suggested.  It will be faster, shorter, and simpler.
0
 
LVL 2

Expert Comment

by:milenvk
ID: 1171496
Well people, I agree with you - I just answered without reading your answers... I just had the answer regardless of whether it's good or bad... What can I do to return the points back? There's a question for thresher_shark in c++ questions worth of 20 points... I think that'll do...
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
SetCurrentDirectory path limit 7 105
Setting nameservers after res_init fails doing res_query 2 86
Add values of each row in an array 3 50
What is sub-make ? 2 39
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

920 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

15 Experts available now in Live!

Get 1:1 Help Now