In this tutorial, we'll look at how to create a directory.
- Directory Handling
- CreateDirectory Windows Function
- Example Program
In the next few tutorials, we'll make programs that would do some basic things in managing directories. In this tutorial, we would make a new directory, if one does not already exist, and display an okay message; otherwise, if the directory already exists, we would display a message box with an error message.
The third parameter of the MessageBox() function is the title for the message box; if that parameter is NULL, a default value of "Error" is used.
Besides this, in the next few tutorials, we would go over handling every item in a directory, changing the current directory, removing a directory, and deleting a file.
So first of all, to make a directory, we would need to use the CreateDirectory() Win32 API function; let's take a look at that.
CreateDirectory Windows Function
The CreateDirectory() Win32 API function makes a directory at the path specified by the first parameter. All the intermediate directories in the path have to already exist, by the time you call this function.
- The path to the new directory.
- An optional pointer to a SECURITY_ATTRIBUTES structure.
If the function succeeds, the return value is non-zero (TRUE).
The return value is zero (FALSE), otherwise.
This function has ANSI and Unicode versions (CreateDirectoryA and CreateDirectoryW; just CreateDirectory won't work).
We would be using the CreateDirectoryA() version of the function.
This function is part of the kernel32.dll DLL. The kernel32.dll DLL contains a lot of the functions that are needed for a program to survive (ie memory allocation, file I/O, etc.).
For more reference to this function, go to CreateDirectory Function (Windows).
Okay, now let's use the CreateDirectory() function.
Use the CreateDirectoryA() Win32 API function to make a new directory, named our_dir.
If successful, display a message box that says so; display an error message box, otherwise.
To display a success message, we would need to compile a string from parts. We would need to copy part 1 of the message to a string, then append the name of the directory to that string, and then append part 2 of the message to the string.
Similar with the error message, except that we use part 1 and part 2 of the error message, for the copying and appending, instead of using the regular message's part 1 and part 2.
If the directory does not already exist, the output should be something like this:
If the directory does exist, an error message should come up. You can remove the directory and try again, in that case.
CreateDirectory Function (Windows)
Intro To Win32 Assembly, Using NASM
More About Arrays
Directory Handling - Directory Listing