Solved how to delete recursive .doc but leave .docx

October 4, 2011 at 20:58:33
Specs: DOS
Hi Experts,

I have this puzzling problem which i could not find any answer.

To delete every doc in a folder i run Del *.doc /s

this however deletes files that are .docx

are there by chance any command or batch file i could use to delete doc but leave docx?

See More: how to delete recursive .doc but leave .docx

Report •

October 5, 2011 at 00:52:59
✔ Best Answer
An interesting question on two counts:

1. Why is *.doc matching, e.g., file.doc1?
2. How to avoid the problem?

That 1 is indeed the case can be seen by doing:

dir *.doc

in a directory containing files 1.doc, 1.doc1, and 1.doc2. The output will be:


Why should that happen? (It doesn't if you type "dir *.do"). I believe the answer lies in the short filenames (8.3 format) that Windows automatically creates. For example, for "1.doc1" there will also be a short filename something like "17126~1.doc". You can see these short names by adding the /X switch to the dir command.

The solution? Try instead:

forfiles /m *.doc /c "cmd /c echo @file"

and you will only get:


The "forfiles" command seems to only match the full filename. So the answer is to use the command:

forfiles /s /m *.doc /c "cmd /c del @file"

either from the command line or in a batch file.

Until your question I never appreciated this behaviour of wildcard matching in some commands. You learn something new every day.

Report •

October 5, 2011 at 15:06:46
I've been googling for an answer for some time now without any results.

Your answer was spot on.
This is the first time i seen forfiles command.thank you ijack.

Report •

October 6, 2011 at 07:09:55
Might want to consider using @path instead of @file.

How To Ask Questions The Smart Way

Report •

Related Solutions

October 6, 2011 at 13:45:21
How tactfully put!

My bad - not "might want to consider using" but "absolutely must use". I didn't test with subdirectories.

Report •

Ask Question