Normally, when I do a copy /b *.data foo in Windows cmd, the *.data files are catenated in the sorted order into the destination file foo. This matches my desired behavior.

However, I just met a curious directory in which this copy /b command will copy the files out of order. The order is not exactly random, but second last file in the sorted order will go first, followed by the remaining files in sorted order.

Is there a way to overcome this strange sorting behavior? More generally, what if I want to copy the files, say, in the reverse order?

  • What's the name of odd file? – Joel Coehoorn Dec 2 '10 at 2:52
  • @Joel The filenames are just nnnnnnnn.data where n is a digit and the numbers are consecutive counting from 1. The odd file is always the second last file in the sorted order. Say if I have 5 files, 00000004.data will come out first. – maverickwoo Dec 2 '10 at 5:20

This will concatenate the files (even the ones with spaces) in reverse order:

C:\> for /f "tokens=*" %i in ('dir /a-d /o-n /b c:\some_dir\*') do @type %i >> c:\another_dir\dest.dat

If there are only a few files, you can list them explicitly:

C:\> copy /b c.dat+a.dat+b.dat dest.dat

Something like this might work in some situations:

C:\> copy /b file*.dat+foo.dat+bar*.dat dest.dat
  • I must learn to type faster! What Dennis said. Although type (or more) will only do an ASCII copy and copy /b is binary. – Rhys Gibson Dec 2 '10 at 4:39
  • @Rhys: I just tested type with binary files and it worked perfectly. – Dennis Williamson Dec 2 '10 at 4:47
  • Thank you. I was also suspicious on typing a binary file but apparently it works as expected on my Windows 7 machine. – maverickwoo Dec 2 '10 at 5:22
  • Great. I stand corrected. – Rhys Gibson Dec 2 '10 at 20:53
  • Note that % needs to be escaped as %% in a .bat file. – Cees Timmerman Oct 10 '14 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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