Retrieve/Iterate all .doc files in a directory
Posted: 09 Aug 2018 04:42
What is the recommended method to get only the files in a directory with the extension .doc?
I often see, and use, examples of For loops like this:or this:
However in the real world that does not do what is asked; in the straight For loop for example:In an example For /F loop:As you can clearly see, I didn't ask for .docm or .docx files, and whilst I accept that in many cases getting all of those extensions can be seen as a bonus, running a process on them instead of Echo could be potentially catasrophic.
I know the Where command is successful:
I often see, and use, examples of For loops like this:
Code: Select all
For %A In (*.doc) Do @Echo %A
Code: Select all
For /F "Delims=" %A In ('Dir /B/A-D *.doc') Do @Echo %A
Code: Select all
C:\Users\Compo>For %A In (Desktop\test\*.doc) Do @Echo %~nxA
FileName01.docm
FileName03.docx
FileName01.doc
FileName02.doc
FileName01.docx
FileName02.docx
Code: Select all
For /F "Delims=" %A In ('Dir /B/A-D "Desktop\test\*.doc" 2^>Nul') Do @Echo %A
FileName01.docm
FileName03.docx
FileName01.doc
FileName02.doc
FileName01.docx
FileName02.docx
I know the Where command is successful:
Code: Select all
C:\Users\Compo>For /F "Delims=" %A In ('Where "Desktop\test":*.doc 2^>Nul') Do @Echo %~nxA
FileName01.doc
FileName02.doc
- Is there a simpler or better recommended pre-processing method of matching only the extension entered?
(I'm aware that there are post-processing methods, e.g. piping the output through FindStr or using If %~xA==.doc etc.) - Why does nobody ever seem to complain that *.doc is matching extensions they didn't ask for?