Overview
- Deleting A File
- Removing A Directory
Deleting A File
Okay, let's remove a directory. But wait... We can't - not yet, that is. In order to remove a directory, it has to be empty; that means that we have to delete everything from the directory first.
How do we delete a file? It's simple; as simple as a function call to DeleteFile().
DeleteFile - Deleting A File
To erase a file, we would need to use the DeleteFile() Win32 API function. DeleteFile(), like a lot of other Win32 API functions, has ANSI and Unicode versions. That means that we would actually need to call DeleteFileA(), in our program.
Parameters:
- The pointer to a string that has the name of the file to delete.
Return:
BOOL
If the function succeeds, the return value is non-zero (TRUE); the return value is zero (FALSE), otherwise.
We won't go too far into how to delete a file, as most of it is understandable, and removing a directory is so much similar to deleting a file; however, I would give a mini example program, in case you need an example of how to use the DeleteFile() function.
So here's the code:
;; Define the externs. extern DeleteFileA extern ExitProcess ;; Construct the symbol import table. import DeleteFileA kernel32.dll import ExitProcess kernel32.dll ;; The following goes into the code section; use 32-bit code. section .text use32 ;; Start program execution here. ..start: push dword the_file call [DeleteFileA] push dword 0 call [ExitProcess] ;; Define the data. section .data the_file db "test.txt", 0
That was a short program, wasn't it? At least compared to some other programs we wrote. We didn't even define a main() function.
Okay, so now that we know how to delete a file (and therefore we can now delete all the files from a directory), we can go on to removing a directory.
Removing A Directory
Removing a directory is almost as easy as deleting a file. When you delete a file, you just make sure it's not in use and call the DeleteFile() function.
When you remove a directory, you have to first remove all its subdirectories and delete all the files that it contains. In our case, however, we already know how to delete files, so for the example I'll just use an empty directory.
And by the way, a directory is another word for folder, in case you didn't know that.
RemoveDirectory - Removing A Directory
To remove a directory, we'll call the RemoveDirectory() Win32 API function. RemoveDirectory(), like DeleteFile(), has ANSI and Unicode versions; we'll use RemoveDirectoryA() (ANSI version), for our example.
Parameters:
- The pointer to the string that contains the name (or path) of the directory to remove.
Return:
BOOL
If the function succeeds, the return value is non-zero (TRUE); the return value is zero (FALSE), otherwise.
As one can clearly see, RemoveDirectory() is very similar to DeleteFile().
Example Program - The Plan
The plan is to try to remove the directory under the name "test" .
If we can't remove the directory, we need to display a message box with an error message.
Otherwise, we display a message box with a message that says that we successfully removed the directory.
Be sure to make the directory "test" before testing this example program!
Example Program - The Code
;; Define the externs. extern RemoveDirectoryA extern MessageBoxA extern ExitProcess ;; Construct our symbol import table. import RemoveDirectoryA kernel32.dll import MessageBoxA user32.dll import ExitProcess kernel32.dll ;; The following goes into the code section; use 32-bit code. section .text use32 ;; Start program execution at this point. ..start: ;; Call the main() function. call main ;; Exit, returning whatever main() returned. push eax call [ExitProcess] main: enter 512, 0 ;; Remove the directory. push dword the_directory ;; The first parameter for the RemoveDirectory() function is the ;; pointer to the string that contains the path/name of the directory to delete. call [RemoveDirectoryA] cmp eax, 0 ;; 0 means error. jz .error ;; If an error happened, go to .error ;; Otherwise, prepare the message string. lea ebx, [ebp-512] ;; Get the address of our string. ;; Copy part 1 of the message to our string. push dword part1 push ebx call strcpy ;; Append the directory name. push dword the_directory push ebx call strcat ;; Append part 2 of the message. push dword part2 push ebx call strcat ;; Display the message box with the prepared message. push dword 0 push dword the_title push ebx push dword 0 call [MessageBoxA] ;; Clear the return value and go to .finish xor eax, eax jmp .finish .error: ;; Prepare the error message. lea ebx, [ebp-512] ;; Get the address of our string. ;; Copy part one of the error message to our string. push dword err_part1 push ebx call strcpy ;; Append the directory name to our string. push dword the_directory push ebx call strcat ;; Append part two of the error message to our string. push dword err_part2 push ebx call strcat ;; Display a message box with the prepared error message. push dword 0 push dword the_title push ebx push dword 0 call [MessageBoxA] ;; Set the return value to -1 and go to .finish mov eax, -1 jmp .finish .finish: leave ret %include "../inc/str.asm" ;; We include the file that defines the string functions. ;; The following goes into the data section. section .data ;; Define the data variables. the_title db "RMDIR", 0 the_directory db "test", 0 part1 db "Success! The directory ", 34, 0 part2 db 34, " was removed. ", 0 err_part1 db "Error. The directory ", 34, 0 err_part2 db 34, " was not removed. ", 0
Example Program - The Output

(Fullsize Screenshot)
References:
DeleteFile Function (Windows)
RemoveDirectory Function (Windows)
First Tutorial:
Intro To Win32 Assembly, Using NASM
Previous Tutorial:
Directory Handling - The Current Directory