Page 1 of 1

Retrieve/Iterate all .doc files in a directory

Posted: 09 Aug 2018 04:42
by Compo
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:

Code: Select all

For %A In (*.doc) Do @Echo %A
or this:

Code: Select all

For /F "Delims=" %A In ('Dir /B/A-D *.doc') Do @Echo %A
However in the real world that does not do what is asked; in the straight For loop for example:

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
In an example For /F loop:

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
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:

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?

Re: Retrieve/Iterate all .doc files in a directory

Posted: 09 Aug 2018 07:43
by aGerman
Compo wrote:
09 Aug 2018 04:42
  • 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.)
I'm not aware of any simple method.
Compo wrote:
09 Aug 2018 04:42
  • Why does nobody ever seem to complain that *.doc is matching extensions they didn't ask for?
Because it doesn't help if you complain :lol: Nothing will change.

Steffen

Re: Retrieve/Iterate all .doc files in a directory

Posted: 09 Aug 2018 12:32
by sst
Only If there was a switch or setting in cmd to ignore 8dot3names on pattern matching. So unfortunately no simpler methods other than the ones you are already aware of until someone discovers that magic word; if it exists at all.

Re: Retrieve/Iterate all .doc files in a directory

Posted: 10 Aug 2018 16:07
by ShadowThief
What version of Windows are you using? I can't replicate your results on Windows 7.

Re: Retrieve/Iterate all .doc files in a directory

Posted: 10 Aug 2018 18:03
by sst
ShadowThief wrote:
10 Aug 2018 16:07
What version of Windows are you using? I can't replicate your results on Windows 7.
Most probably short file names creation is disabled either on the volume you are testing or globally through registry. In this case DIR /X should not display short file names for newly created files.
Or query by

Code: Select all

fsutil 8dot3name query <NtfsVolumeDriveLetter>:

Re: Retrieve/Iterate all .doc files in a directory

Posted: 11 Aug 2018 03:52
by Compo
ShadowThief wrote:
10 Aug 2018 16:07
What version of Windows are you using? I can't replicate your results on Windows 7.
I have confirmed the behavior on several different OS's over the years, and agree with sst, that you must have modified settings on your Windows 7 system.

My settings are the defaults, 0 for the volume state, and 2 for the registry state.