I'm using the copy command in command prompt to merge all the files in a directory, for a number of directories.

The problem is, I need to edit the first file in each directory before I merge. This means that when I put in the command "copy /b *.mp3 name.mp3", the joined file has part 2 at the start and part 1 at the end, presumably because it was created last.

Is there a way of using the copy command so that the files merge in name order? Each folder has a different number of parts, anywhere from 2 to 1000 so I don't want to list each file with a "+" in between.

Ideally, I'd like to find something to insert into the copy command I'm already using. Otherwise, is there a way of rearranging the files in a folder so that if you enter "DIR", part 1 shows up first even if it was edited last?

  • add windows and/or windows-7 tag – rems Feb 23 '11 at 17:29

There are ways to write a batch file which will do this iteratively, but one (of many) simple solution would be to pipe the output of the directory command to a text file, create a batch file to copy a single item passed on the command line to the desired file, and then open the text file and paste the command to call the batch file at the beginning of each line.

dir /b *.mp3 > filelist.txt


dir /b /s *.mp3 > filelist.txt

/b means brief output, just filenames; /s means subdirectories too; ">" means "send output to"

batch file to copy named copymp3.bat:

copy /b name.mp3 + /b %1 name.mp3

The %1 means "use first command line parameter passed".

Sort the list however you like, then edit filelist.txt so each line looks like this:

call copymp3.bat "name of first file in list.mp3"

The "call" command says to run the batch file and wait for execution to finish before proceeding to next command. Quotes are needed around each filename because the spaces will otherwise signal mulitple parameters rather than one single one. Before you add the command at the beginning, do a "find and replace" for .mp3 and replace with .mp3"

Rename the filelist.txt file filelist.bat and then it will execute if you dbl-click.

google "batch file cmd file" for more information.

If you have thousands of files, this will take a while to construct the batch file. A find and replace editor which can recognize end-of-line would be quite useful.

TEST FIRST on a small subset


The easiest method is to use a temporary directory.

  1. Create a temporary directory (a subdirectory in the directory where the target files are located is usually good)
  2. In Explorer, click the Name column to sort the files by name
  3. Select all the required files
  4. Move the selected files to the temporary directory (ie cut-paste them or use another move command)

If you do a dir in the temporary directory you will see that the directory-order is alphabetical (ie, dir == dir /on) because that was the order in which Explorer moved them. You can do the same thing to force directory-order to be by date, size, or any other column that Explorer has. At this point, you can either perform your action in the temporary directory then move them back, or just delete the old directory (be careful if the temp one is inside the old one) and rename the temporary one and move it to where the old one was.

One caveat though, you cannot simply sort and move the files in and out of the same one or two directories because old directory entries for files can get reused, resulting in what seems like the directory "remembering" the old order regardless of how you sorted them later. That's why you need to create a new temporary directory to use because it has no entries yet (ie, no deleted files).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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