create directory

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.
tariq6000Asked:
Who is Participating?
 
sunnycoderConnect With a Mentor Commented:
Hi tariq6000,

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

Cheers!
sunnycoder
0
 
itsmeandnobodyelseConnect With a Mentor Commented:
>>>> how can we check this.

#include <sys/stat.h>


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


Regards, Alex
0
 
jkrConnect With a Mentor Commented:
The Windows way:

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

  // does not exist
}

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

  // does exist, could also be a file
}
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
BrianGEFF719Commented:
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
 
jkrCommented:
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
 
BrianGEFF719Commented:
>>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
 
jkrCommented:
... and regarding that, you are absolutely right ;o)
0
All Courses

From novice to tech pro — start learning today.