Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
What's the name of odd file? – Joel Coehoorn Dec 2 '10 at 2:52
@Joel The filenames are just 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, will come out first. – maverickwoo Dec 2 '10 at 5:20
up vote 2 down vote accepted

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
share|improve this answer
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .