Solved

create directory

Posted on 2006-11-07
9
201 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

776 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