Dos - Delete all directories with pattern

By xngo on November 26, 2019

Let's assume that I want to delete all directories with starting name Windows in C:\Program Files (x86) folder. Here is the batch script to do this.

@ECHO OFF
FOR /F "delims=" %%W IN ('dir /d/b "C:\Program Files (x86)\Windows*"') DO (
    ECHO "C:\Program Files (x86)\%%W"
    REM RMDIR /s/q "C:\Program Files (x86)\%%W"
)

Note: For safety reason, I commented out the rmdir command. If you want it to delete folders, remove REM.

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.