Delete folders in command promote with wildcards

Status
Not open for further replies.

Catsrules

Distinguished
Dec 6, 2008
689
0
19,060
Is there a way to Delete multiple folders, and use wildcards as well in command prompt?
For example, I have a folder with hundreds of folder, and I want to delete all of them that start with a 9.

Thanks
 
Solution
Whoops, forgot that "rmdir" doesn't accept wildcards.

Try this:

for /D %f in (9*) do rmdir %f /s

That command should work if you type in in directly at the Command Prompt window. If you want to put it into a ".bat" or a ".cmd" file, you need to double the "%" characters, as in:

for /D %%f in (9*) do rmdir %%f /s

As above, add "/q" to suppress the confirmation prompts.
rmdir 9* /s

add "/q" to suppress the confirmation prompt.


You can easily do this in Explorer as well - just sort by folder name, click on the first folder whose name starts with "9", shift-click on the last folder whose name starts with "9", and press the "Delete" key.
 



Didn't work, I made a dummy folder with a bunch of folders, some starting with 9 and others with other numbers and letters.

It did rmdir 9* /s
and it get "The filename, directory name, or volume label syntax is incorrect".

I was thinking about putting this in a script, so I can run it easily one 30+ computers, that is why I don't want to do in in Explorer.
 
Whoops, forgot that "rmdir" doesn't accept wildcards.

Try this:

for /D %f in (9*) do rmdir %f /s

That command should work if you type in in directly at the Command Prompt window. If you want to put it into a ".bat" or a ".cmd" file, you need to double the "%" characters, as in:

for /D %%f in (9*) do rmdir %%f /s

As above, add "/q" to suppress the confirmation prompts.
 
Solution
Yeah basically the "for" command lets you execute whatever command you write after "do" multiple times, once for each file found in the "(...)" expression. The found file name is put into the "%f" variable and can be used as part of the command. "/D" tells it to look for directories - normally it will only find files.
 



Very cool!

Thanks for the Education.
 
In these "modern times" Powershell would be the "Coolest" way to do this. :bounce:

Change current directory to the folder where the files to be deleted exists
(or specify in commandline "c:\MyFiles\9*")

Option 1:
Start Windows Powershell instead of cmd.exe and run command below:
Remove-Item 9* -recurse -force

Option 2:
from Cmd/commandline..

powershell.exe Remove-Item 9* -recurse -force

Cheers!

Vidar
 


How and where do i put in a searchpatch for the folders?
I have securitycameras recording videosfiles saved in folders with name of date och folder inside with time. on a specific place on my hard drive.
For now i use a .cmd file with this commands (del /q /s D:\Users\Shares\DPFtp\hall\)
Buit it only delete the files it selves, but not the folders.

Is it a workaround this problem?

 
Status
Not open for further replies.