Windows C++ : Create New Directory

This post gives you the basic idea about creating a new directory in windows using the function CreateDirectoryA() in C++.

You are free to use any IDE you want to But the preferred ones are Microsoft Visual Studio or CodeBlocks with GCC compiler or any other proper C++ compiler properly installed.

Let's get started by creating a  new project, Select Console Application (windows).
Add a .cpp file to the project, Let us name it main.cpp.

The Header Files we use

  • iostream.h
  • windows.h

CreateDirectoryA()

CreateDirectoryA() is the function used to create a new directory in windows.

Syntax 

BOOL CreateDirectoryA(
  LPCSTR                lpPathName,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes
);

BOOL is the return type which has two values either TRUE or FALSE. If the directory is successfully created the function returns a TRUE or else FALSE.
LPCSTR is the data type of the first parameter. lpPathName is the path of the directory to be created (along with the new directory name).

LPSECURITY_ATTRIBUTES is the data type of the second parameter. lpSecurityAttributes pointer to a SECURITY_ATTRIBUTES structure. You can read more about it from the official link to Microsoft provided. Generally, the value of lpSecurityAttributes is taken as NULL and that is what we are also going to take here.

PROGRAM TO CREATE NEW DIRECTORY

 #include <iostream>  
 #include<windows.h>  
 using namespace std;  
 int main()  
 {  
   BOOL bDir;  
   LPCSTR lpPathName="C:/MY NEW FOLDER";  
   bDir= CreateDirectoryA(lpPathName, NULL );  
   if(bDir==TRUE)  
   {  
     cout<<"Directory created Successfully\n";  
   }  
   else  
   {  
     if(GetLastError()==3)  
     {  
       cout<<"Error: Path not found\n";  
     }  
     else  
     {  
       cout<<"Error: File already exist\n";  
     }  
   }  
   system("PAUSE");  
   return 0;  
 }  


As you can see bDir is the BOOL variable which gets the reutrn value from CreateDirectoryA(). 

IpPathName  variable is given a directory address to my C directory and to create a new directory inside it naming MY NEW FOLDER. You may have noticed that when you copy a directory address in Windows, By default we get C:\ for C drive, But in the program instead of backslash '\' frontslashes '/' are used. This is because '\' is a special character and to use it we should put '\\' for one \. But windows don't mind using frontslash '/' instead of '\' so it is used in the program.

CreateDirectoryA() is called with required parameters.

If statement checks if CreateDirectoryA() is called successfully. If MY NEW FOLDER is created, "Directory Created successfully" will be printed.

GetLastError() 

On creating a ne directory there are two posibble errors to occur.
  1. Invalid path or Path not found.
  2. There is already a directory with same name.
For Invalid path GetLastError will return 3, So we check it with an if statement. Since there are only two errors possible the other value returned will be for having a directory with same name.

I hope you understood how CreateDirectoryA() function works to create new directory. If you have any questions or doubts regarding this feel free to ask it by commenting below.

Jobin Jose

1 comment:

  1. "else file already exists"?? You should check out https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessage

    ReplyDelete