Solved

create directory

Posted on 2006-11-07
9
200 Views
Last Modified: 2010-04-01
if we want to create directory
we write:

BOOL create= CreateDirectory(m_pcFolderName,NULL);

But if directory already exist at destination
how can we check this.
0
Comment
Question by:tariq6000
9 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 168 total points
ID: 17888502
Hi tariq6000,

If directory already exists, CreateDirectory will return 0 and GetLastError would get you ERROR_ALREADY_EXISTS error

Cheers!
sunnycoder
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 166 total points
ID: 17888605
>>>> how can we check this.

#include <sys/stat.h>


    struct stat status;
    if (stat(m_pcFolderName, &status) == 0)  // directory exists


Regards, Alex
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 166 total points
ID: 17892208
The Windows way:

if ( -1 == GetFileAttributres(m_pcFolderName)) {

  // does not exist
}

f ( -1 != GetFileAttributres(m_pcFolderName)) {

  // does exist, could also be a file
}
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 19

Expert Comment

by:BrianGEFF719
ID: 17895793
To expand on what JKR has said you can do:



#define FILE_DIRECTORY 0x10
#define NO_EXIST 0xFFFFFFFF

int folderExist( char *folder)
{
      DWORD fAttr = GetFileAttributes(folder);
      if (fAttr == NO_EXIST)
            return 0; /* return 0 if nothing exists */
      else if ( fAttr & FILE_DIRECTORY )
            return 1; /* return 1 for directory that exists */
      else
            return 2; /* return 2 for file */

}
0
 
LVL 86

Expert Comment

by:jkr
ID: 17895806
That does not mak any difference - if either a directory or file with the same name is already there, the creation will fail, that's why I omitted that.
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 17895834
>>That does not mak any difference

I know,I didnt consider my post 'for points' anyway, I was just point out that you can explicitly check for a folder.
0
 
LVL 86

Expert Comment

by:jkr
ID: 17895843
... and regarding that, you are absolutely right ;o)
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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 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

20 Experts available now in Live!

Get 1:1 Help Now