Retrieve/Iterate all .doc files in a directory

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Compo
Posts: 488
Joined: 21 Mar 2014 08:50

Retrieve/Iterate all .doc files in a directory

#1 Post by Compo » 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:

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?

aGerman
Expert
Posts: 3786
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 09 Aug 2018 07:43

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

sst
Posts: 79
Joined: 12 Apr 2018 23:45

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

#3 Post by sst » 09 Aug 2018 12:32

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.

ShadowThief
Expert
Posts: 910
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

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

#4 Post by ShadowThief » 10 Aug 2018 16:07

What version of Windows are you using? I can't replicate your results on Windows 7.

sst
Posts: 79
Joined: 12 Apr 2018 23:45

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

#5 Post by sst » 10 Aug 2018 18:03

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

Compo
Posts: 488
Joined: 21 Mar 2014 08:50

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

#6 Post by Compo » 11 Aug 2018 03:52

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.

Post Reply