Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 454
  • Last Modified:

Checking directory existance

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
cwirrgan
Asked:
cwirrgan
  • 7
  • 4
  • 3
  • +1
1 Solution
 
nietodCommented:
this is platform dependant.  What OS are we taling about?
0
 
thresher_sharkCommented:
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
 
thresher_sharkCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
thresher_sharkCommented:
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
 
milenvkCommented:
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
 
nietodCommented:
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
 
thresher_sharkCommented:
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
 
cwirrganAuthor Commented:
The platfom is Windows NT/95
0
 
thresher_sharkCommented:
milenvk - Isn't that a little overkill considering what I proposed?
0
 
thresher_sharkCommented:
cwirrgan - Will access work in that case?
0
 
nietodCommented:
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
 
cwirrganAuthor Commented:
Access will work in my case.
0
 
cwirrganAuthor Commented:
Access will work in my case.
0
 
thresher_sharkCommented:
Oh, yes you are right nietod.  Thank you for clarifying.
0
 
nietodCommented:
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
 
milenvkCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 7
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now