Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

From within a DOS batch file, how can I crudely re-sort a text file containing a manually maintained list of source files, so those files listed (from the same directory) in the small example extract below are grouped together again? I don't like how the DOS sort.exe reorders them in pure alphabetical order, where ctype.h and assert.h have been separated by files listed in the c++ sub-directory.

I'd prefer to do this using pure Windows XP/DOS (or Unix) commands - or if needed download an existing executable utility that doesn't need installing like tcl or perl scripts. It's a pity dir /s /b can't take input from file trying to use it as a format filter, not sure if ls can.

To be reordered after DOS sort.exe has done its mischief:

C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\ar.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\argz.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\assert.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\auto_ptr.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\backward_warning.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\binders.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\boost_shared_ptr.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\functional_hash.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\hashtable_policy.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\cr_section_macros.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\ctype.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\dirent.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\envlock.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\envz.h

Desired output:

C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\ar.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\argz.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\assert.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\cr_section_macros.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\ctype.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\dirent.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\envlock.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\envz.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\auto_ptr.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\backward_warning.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\binders.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\boost_shared_ptr.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\functional_hash.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\hashtable_policy.h
share|improve this question
    
Windows XP does not use DOS. Do you mean cmd ("Command Prompt")? –  grawity Mar 25 '11 at 15:56

1 Answer 1

I don't believe this is a very elegant solution but it does work:

  1. Download the GnuWin32 CoreUtils package
  2. Extract the files somewhere accessible.
  3. Use the 'sort' executable provided along the lines of:

    sort <input file> -t \ -k 9 -k 8 -k 7 -k 6 -k 5 -k 4 -k 3 -k 2 -k 1
    

This assumes a maximum depth in your files of 9 (9 fields separated in your paths by the back slash character). The output is:

C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\ar.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\argz.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\assert.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\cr_section_macros.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\ctype.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\dirent.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\envlock.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\envz.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\auto_ptr.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\backward_warning.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\binders.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\boost_shared_ptr.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\functional_hash.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\hashtable_policy.h

If the paths were any longer than provided in your example you'd basically have to add further -k switches to ensure you keep your desired sorting.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.