Windows – Remove folders by name from command-prompt

command linewindows 7

How can I remove all folders and sub folders containing SQ00 in their name from the command-prompt?

Best Answer

First go to the path where you would want to originate the search/delete.

cd c:\temp

Then type the following:

for /f %i in ('dir /a:d /s /b *sq00*') do echo rd /s /q %i

When you see the output and you are comfortable it would delete the folder(s) you see, you can remove the echo and actually run the command like this:

for /f %i in ('dir /a:d /s /b *sq00*') do rd /s /q %i

dir /a:d /s /b *sq00* looks through the directory tree where you originate the command (current directory!) and all subfolders matching folders including the string sq00. /a:d means it only looks for directories, /s means it looks recursively, /b means "bare" format excluding headers/etc.

For each result it finds with dir, it puts it in the variable %i and then runs the command rd /s /q %i on it. rd deletes folder (short for rmdir), and the flags /s means that it removes the folder and all subfolders, and finally /q means it is quiet and won't ask any questions.