Solved

create directory

Posted on 2006-11-07
9
199 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

757 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

16 Experts available now in Live!

Get 1:1 Help Now