rd/rmdir - Delete a folder
When working with command prompt or batch files, it is common to manipulate files and folders on your PC. However, there are different commands to use depending on whether you want to delete a file or a folder, and whether the folder is empty or not.
In this article, we will explain how to delete a folder using the rmdir
and rd
commands, from basic usage to setting options. Please note that a different command is needed to delete a file.
For more information on deleting files, please refer to our article on the del
command.
How to Delete a Folder
To delete a folder, use the rmdir
command.
You can also use the rd
command instead.
The rmdir
command is used as follows:
rmdir [<Drive>:]<Folder_Path> [/s [/q]]
The most basic usage is to specify the path of the folder you want to delete after rmdir
.
The following command is an example of deleting the test
folder created directly under the user directory.
By executing this command, the test
folder will be deleted.
Note that the rmdir
command assumes that the folder you want to delete is empty.
We will explain how to delete a non-empty folder later.
Options for the rmdir
Command
The rmdir
command has the following options:
Option | Description |
---|---|
/s | Deletes files in the specified folder as well |
/q | Does not display a confirmation message for deletion |
By specifying the /s
option, you can also delete files in the specified folder.
When /s
is specified, a confirmation message for deletion will be displayed at runtime as follows:
If you do not want to display this message, specify the /q
option in addition to /s
.
When using the rmdir
command in a batch file, the program will stop at the confirmation message, so it is recommended to specify the /q
option.
How to Delete a Folder with Examples
Deleting a Temporary Folder in a Batch File
Consider a case where a temporary folder is created in a batch file to perform various operations.
In such a case, you need to delete the temporary folder after the work is done, and you can use the rmdir
command for that purpose.
@echo off
setlocal
mkdir workspace
rem ...perform operations here...
rmdir workspace /s /q
endlocal
As there is a possibility that files are included in the temporary folder, the /s
and /q
options are specified.
Delete if the specified path is a folder
To accept user input in a batch file and delete the input path if it is a folder, you can write the following:
@echo off
setlocal
echo Please enter the path of the folder you want to delete:
set /p path=
if exist %path% (
rmdir %path% /s
echo %path% has been deleted.
) else (
echo %path% does not exist.
)
endlocal
If you execute the above code, you will be prompted to input the folder you want to delete and a confirmation message will be displayed before the deletion.