Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete folders with wild cards from a batch file Windows 7

  1. I know we can write programs to do it.
  2. I know we can write other scripts (perl/vbscript/ etc.) to do it.

I am looking for a command prompt/batch file solution to delete all folders matching sub_* (like sub_1, sub_2 ... ) to be deleted. rmdir or rd doesn't support wildcards, and I'm not able to figure out how to pipe the output of dir sub_*/ad command to delete command one by one as well. Any loop syntax etc. I can use?

like image 984
Piyush Soni Avatar asked Feb 05 '11 00:02

Piyush Soni


2 Answers

for /d %x in (sub_*) do rd /s /q "%x"

You need to double the % if used in a batch file:

for /d %%x in (sub_*) do rd /s /q "%%x"

Untested, make sure to first use echo or something else that doesn't immediately wipe the directories ;)

like image 99
Joey Avatar answered Sep 27 '22 20:09

Joey


forfiles /P C:\where\my\dirs\at /M sub_* /C "cmd /c if @isdir==TRUE rmdir /s /q @file"

like image 44
JJS Avatar answered Sep 27 '22 20:09

JJS