Combine Text Files Using The Windows Command Prompt

About once a year I find the need to combine multiple files quickly in a Windows environment. Here’s an example where I combine six CSV files into one. The files are made up of randomly generated names:

  1. Form of address
  2. First Name
  3. Middle Name
  4. Last Name
  5. Suffix
  6. Sex

Each file contains a header row. You can access this example set here.

From the command prompt, navigate to the location of the files. Here’s the result of a dir command for my example folder.

09/15/2017  09:04 AM             1,671 names-1.csv
09/15/2017  09:04 AM             1,664 names-2.csv
09/15/2017  09:04 AM             1,660 names-3.csv
09/15/2017  09:04 AM             1,732 names-4.csv
09/15/2017  09:04 AM             1,699 names-5.csv
09/15/2017  09:04 AM             1,708 names-6.csv

You could use the type command and the >> redirection operator, which would result in combining all of these files:

>type *.csv >> output.csv

This results in a less-than-ideal output file, because the header row from every file is repeated. You can remove these manually, but if you’re combining dozens or hundreds of files, or if you’re looking to automate a task, the more command may be a better solution.

The more command is similar to the type command but includes an option to start n lines from the top of the file.

Consider a file named example-shortlist.csv that looks like this:

FOA,First,Middle,Last,Suffix,Sex
Ms,Gayle ,Hannah,Abbott,,Female
Mr,Travis ,Sherwood,Abbott,,Male
Ms,Velma ,Cecelia,Adams,,Female
Ms,Nora ,Carey,Adkins,,Female
Mr,Blaine ,Hassan,Aguilar,,Male

The more command with no parameters would look like:

>more example-shortlist.csv

Output:

FOA,First,Middle,Last,Suffix,Sex
Ms,Gayle ,Hannah,Abbott,,Female
Mr,Travis ,Sherwood,Abbott,,Male
Ms,Velma ,Cecelia,Adams,,Female
Ms,Nora ,Carey,Adkins,,Female
Mr,Blaine ,Hassan,Aguilar,,Male

Now with the +n parameter:

>more +1 example-shortlist.csv

Output:

Ms,Gayle ,Hannah,Abbott,,Female
Mr,Travis ,Sherwood,Abbott,,Male
Ms,Velma ,Cecelia,Adams,,Female
Ms,Nora ,Carey,Adkins,,Female
Mr,Blaine ,Hassan,Aguilar,,Male

This command will combine all csv files in the directory, excluding each file’s header row, into output.csv:

>more +1 *.csv >> output.csv

Output.csv won’t have any header row, though. If you’re automating this task and you want the first file you send to output.csv to include headers, and subsequent files to exclude them, I do not recommend using the Windows Command Prompt. It’s kludgy, and while you can definitely write a for loop with some if/then logic to selectively type out just one file and then more +1 the rest, it would be much faster to use PowerShell. Check out this StackOverflow thread for more information.