Solved

create directory

Posted on 2006-11-07
9
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

615 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