Home » How to Check if a Directory is Empty in CMD

How to Check if a Directory is Empty in CMD

You can use the dir command followed by the path and the /A /B flags in the command prompt to check if any directory on your system is empty.

dir "directory_path" /A /B

In this example, the dir command with /A /B flags lists all files and subdirectories in the specified directory. If the output is empty, the directory is empty.

You can use the batch script to check and display a message for the empty directory.

The following example shows how to use this syntax in practice.

Example: How to Check if a Directory is Empty in CMD

Suppose that we want to check if the following directory is empty:

C:\Users\admin\Documents\apps

We can type the following command to list all files and subdirectories:

dir "C:\Users\admin\Documents\apps" /A /B

If the output is empty, the directory is empty.

To automate this check, we can use the following batch script, and save the file as app-directory-check.bat on C drive.

@echo off
set "directory_path=C:\Users\admin\Documents\apps"

dir "%directory_path%" /A /B >nul 2>&1
if %errorlevel% == 0 (
    echo Directory is not empty.
) else (
    echo Directory is empty.
)

Output: 👇️

CMD - Check if a directory is empty
CMD – Check if a directory is empty

After running the batch file, we can see that the script checks if C:\Users\admin\Documents\apps is empty and displays the appropriate message.

Note: You can use the dir command followed by the path and the /A /B flags to check if any directory on your system is empty.

Conclusion

I hope the above article on checking if a directory is empty in CMD using the dir /A /B command is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.