Iterate over all files in dir tree in alphabetical sort orde

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Iterate over all files in dir tree in alphabetical sort orde

#1 Post by pstein » 30 Mar 2014 01:37

For another task I am searching for a way to iterate over all files with the extensions
*.pdf
*.doc
*.txt
in a certain directory tree with e.g. the top node

D:\sample\foo\bar\

The files should be taken in alphabetical sort order (including the path name).
Within the loop some tasks should be applied for each (current) file.

How can I achieve this?

Thank you
Peter

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Iterate over all files in dir tree in alphabetical sort

#2 Post by foxidrive » 30 Mar 2014 03:19

Try this and see if the output is what you need, to begin with.

Code: Select all

dir "D:\sample\foo\bar\*.pdf" "D:\sample\foo\bar\*.doc" "D:\sample\foo\bar\*.txt" /b /a-d |sort >tempfilelist.txt

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

Re: Iterate over all files in dir tree in alphabetical sort

#3 Post by Compo » 30 Mar 2014 03:27

The DIR output should be in alphabetic sequence by default.

Code: Select all

@PUSHD D:\SAMPLE\FOO\BAR
@DIR/B/S/AD *.DOC *.PDF *.TXT

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: Iterate over all files in dir tree in alphabetical sort

#4 Post by pstein » 30 Mar 2014 03:49

Thank you.
But I need a loop where each file is selected at a time.

For simplicity the file should be output by just an echo command.

How can I put the suggested dir cmds into a loop?

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

Re: Iterate over all files in dir tree in alphabetical sort

#5 Post by Compo » 30 Mar 2014 04:54

Here you go:

Code: Select all

@PushD D:\Sample\Foo\Bar
@For /f "tokens=*" %%A In ('DIR/B/S/A-D *.DOC *.PDF *.TXT') Do @(
   Echo=%%A&&TimeOut /t 1 >Nul)

Post Reply