Windows OS

How to Solve “The directory is not empty” Error When Running rmdir Command in a Batch Script?

The component of the computer system that maintains a group of files or folders is a directory. In the Windows system, directories are also known as folders. These directories are defined by the users or system administrator. Sometimes, we want to remove the directory to free system space, or it may affect our system. You can remove the directory directly from the GUI method as most people prefer to use it. But many of the system administration tasks are done using the Windows command line.

Occasionally, we face the “The directory is not empty” error when we try to remove the directory from the command line or use a batch file. The reason behind it is that we are trying to delete a directory that is not empty but contains files, or it may be corrupted.

This study will provide the solution to resolve the specified error on Windows:

So, let’s start!

How to Solve “The directory is not empty” Error When Running rmdir Command With a Batch Script?

The file that stores a series of commands to execute them in the sequence is known as a Batch script or file. Most of the time, we get the “The directory is not empty” error because we are trying to delete a directory that is not empty or has some error files:

To resolve the above display error, follow the listed steps.

Step 1: Create a Batch File

Open the folder where you want to create a Batch file, and right-click anywhere in the directory. Select the “New” option from the context menu and select “Text Document” to create a simple text document:

To make a simple text file as a batch script, rename the file and change the “.txt” extension to “.bat”. For instance, we have renamed the file as “Delete.bat”:

Step 2: Write Script to Resolve Error and Remove Directory

After creating a simple batch file, add commands to it to remove the directory, and resolve the specified error. For this purpose, right-click on the newly created batch file, and select “Edit” from the displayed options:

Upon doing so, the file will automatically open in Notepad. Paste the provided code there and specify the directory name you want to remove:

@echo Removing Directory

rmdir /s /q My-Directory

Pause

The above-provided commands will resolve the specified error. We have used “rmdir” to remove the directory, the “/s” will remove files and sub-directories recursively, and the “/q” parameter will delete the files in quiet mode without prompting any confirmation:

Step 3: Execute Batch Script

Double click on the newly created batch file to execute it:

Here you can see that we have successfully deleted the directory and resolved the specified error:

How to Solve the “The directory is not empty” Error When Running rmdir Command in Command Prompt?

Removing directories and files is quite easier using Windows graphical user interface. But, most of the Windows administrative tasks are done through the command line.

To resolve the specified error using Windows Command Prompt, follow the below-listed guide.

Step 1: Open Windows Command Prompt

Launch a Command Prompt as an admin user by typing “CMD” in the Start menu:

Step 2: Open Folder Where the Targeted Directory Exists

Open the folder where the directory you want to remove is placed and specify it as target directory:

Step 3: Remove Files and Directorates of Targeted Directory

Use the below-provided command to delete files and folders of the target directory. The main reason for the error is that the targeted directory contains some files or directorates which are needed to remove first, then we can remove the main or targeted directory:

> del /f /s /q My-Directory

Step 4: Remove Directory

Now, simply execute the “rmdir” command to remove directory:

> rmdir My-Directory

The error-free output indicates that we have resolved the specified error.

Alternatively, you can utilize the same command that you have used in the batch script:

Step 5: Verify the Removal of Directory

Utilize the “dir” command to display the list of files and folders. Here, you can see that we have successfully removed the targeted directory:

We have compiled the different solutions to resolve the “The directory is not empty” error.

Conclusion

The error “The directory is not empty” may occur when the user tries to delete a directory that is not empty and contains some files or sub-directories. The specified error can be resolved using the “rmdir” command with the option “/s” that will remove the file and folder first, then the main directory. For instance, you can utilize the “rmdir /s /q <Directory Name>” command. We have offered feasible solutions to fix the specified error on Windows in this blog.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.