PCI-J
PCI-J

Reputation: 43

CMD command to recursively list all files in a folder with a sortorder without regard to location in a hierarchy?

I've been trying to come up with a way to use DIR list all the files on a drive according to their date, but my issue is that the resulting list still goes folder to folder alphabetically, just as one would expect. Within the folders, the files are listed properly, that's not the problem. Cue possibly convoluted visuals.

This is the plain recursive directory listing (with a date column just because that happens to be what I want to sort against):

main                                     Date
 |                                       -----------------
 | -- file0_1.ext                        1/5/10 12:00:00AM
 | -- file0_2.ext                        1/1/10 12:00:00AM
 + -- sub1
 |     | -- file1_1.ext                  1/7/10 12:00:00AM
 |     | -- file1_2.ext                  1/4/10 12:00:00AM
 + -- sub2
 |     | -- file2_1.ext                  1/6/10 12:00:00AM
 + -- sub3
 |     | -- file3_1.ext                  1/2/10 12:00:00AM
 |     + -- sub3_1
 |     |     | -- file3_1_1.ext          1/3/10 12:00:00AM

When I run

dir *.* /s/od

to list all files recursively sorted by date, I get

main\file0_2.ext                         1/1/10 12:00:00AM
main\file0_1.ext                         1/5/10 12:00:00AM
main\sub1\file1_2.ext                    1/4/10 12:00:00AM
main\sub1\file1_1.ext                    1/7/10 12:00:00AM
main\sub2\file2_1.ext                    1/6/10 12:00:00AM
main\sub3\file3_1.ext                    1/2/10 12:00:00AM
main\sub3\sub3_1\file3_1_1.ext           1/3/10 12:00:00AM

where the folders are listed alphabetically, but the files are indeed correctly sorted by date within their folders. However, what I'm really shooting for is:

main\folder\file0_2.ext                  1/1/10 12:00:00AM
main\sub3\file3_1.ext                    1/2/10 12:00:00AM
main\sub3\sub3_1\file3_1_1.ext           1/3/10 12:00:00AM
main\sub1\file1_2.ext                    1/4/10 12:00:00AM
main\file0_1.ext                         1/5/10 12:00:00AM
main\sub2\file2_1.ext                    1/6/10 12:00:00AM
main\sub1\file1_1.ext                    1/7/10 12:00:00AM

where the files are listed regardless of their location in the hierarchy according to their date.

One workaround is to pipe all the output from a listing to a text file, parse it, sort it properly, and list it all again, but I'm really looking for a single command I can run within cmd.exe, so it can be easily implemented from memory on other Windows machines.

Upvotes: 4

Views: 7195

Answers (1)

MC ND
MC ND

Reputation: 70943

From memory

robocopy "c:\main" "c:\main" * /l /nocopy /is /s /njh /njs /nc /ns /ts | sort

That is, call robocopy checking files from the intended folder against the same folder, but only list files, do not copy anything, include same files, process subdirectories, without job header, without job summary, without class, without size, with time stamp, and pipes the output to sort to get the list.

If you only need the list of files

for /f "tokens=2,*" %a in ('robocopy "c:\main" "c:\main" * /l /nocopy /is /s /njh /njs /nc /ns /ts ^| sort') do @echo %b

Upvotes: 3

Related Questions