views:

1446

answers:

7

Greetings,

From the screenie below we can see a directory ordered by Name in Windows Explorer:

*Edit: Didnt work -> * Link

If I try the same thing in MS-DOS it orders by name differently - correctly:

dir *.jpg /ON /B

cubierta.jpg
pag00.jpg
pag06.jpg
pag08.jpg
pag09.jpg
pag100.jpg
pag101.jpg
pag102.jpg
pag103.jpg
pag104.jpg
pag105.jpg
pag106.jpg
pag107.jpg
pag108.jpg
pag109.jpg
pag11.jpg, etc, etc, etc, ...

Is there a way to get MSDOS to order by Name where it reads the numbers as a human would do? Thanks!

A: 

No, there's no way to do this. Windows Explorer uses a different approach to handle this.

Biri
+3  A: 

Your best option (if possible) is to add enough leading zeros (two, in this case) to your smaller numbers so that the sort does come out as expected.

Doug L.
If I _ever_ anticipate needing to process files in order, I pad with 1 to 4 leading zeros or use a complete UTC format timestamp, 20081028143531. This is an *old* trick, dating back to the late '70s or early 80s. Maybe before then...
Ken Gentle
A: 

Write a console app, based on a human sort API. Similar to this post

kenny
+1  A: 

Windows Explorer uses a special sorting function, StrCmpLogicalW, which is not used by cmd.exe, hence the differences in the output. You could write your own sorting program with it, though.

You might also be interested in Michael Kaplan's entries on this subject (from Raymond Chen's suggestion box).

Romulo A. Ceccon
A: 

Windows Explorer uses a different approach to handle this

add enough leading zeros

Thanks people, I didn't know that it was so involved!

ian_scho
A: 

4NT/TCC/TC from JPSoft use natural sort order by default; alternately, if you have access to a GNU "sort" program, such as the Cygwin tools, you can do something like "ls -1 | /bin/sort -g" or "dir /b | \cygwin\bin\sort -g"; that will give you the filenames in natural sort order.

If you are constrained to tools that are native to windows, you can try the Windows Scripting tools and do something like use a FileSystemObject to get the filenames , and regular expressions to extract the numeric part of the filenames to use as keys in a Dictionary object. I'll leave the actual coding as an exercise to the reader :)

A: 

He he he. Do you all want to know my solution?

I'm using a program called ImageMagic that converts my 200MB PDF files to JPG. At the end of the lengthy process it serializes all of the files in numerical order... So basically the file creation date increases in value slightly as each file is being written to the hard drive. Thus either of the following two batch file commands will write the files in 'correct' order:

dir *.jpg /ODN /B > files.txt

for /f "tokens=*" %%a in ('dir *.jpg /ODN /B') do (
    echo ^<page^>%%a^</page^> >>pages.xml.fragment
)

Thus the dir *.jpg /OD command orders the directory content by ascending (creation?) date, and we can completely ignore the actual file name.

ian_scho
I'm pretty sure that last-modification date (not creation date) is used for 'dir /od'. For creation date, you need 'dir /tc /od'.
system PAUSE