28

I am trying to take a folder that has several .csv files in it and combine all of these files and the information in them, into one file using MS DOS. Any suggestions?

edmon
  • 321
  • 2
  • 7
  • 10

7 Answers7

47
copy *.csv new.csv

No need for /b as csv isn't a binary file type.

marto
  • 4,402
  • 25
  • 15
  • how do you undo this? See my question here: http://stackoverflow.com/questions/37357481/undo-a-botched-command-prompt-copy-which-concatenated-all-of-my-files – papiro May 25 '16 at 21:30
  • 3
    Note. This does not work in powershell so make sure you're using cmd. Works perfectly using classic command prompt! – K-Dawg Jul 29 '18 at 14:39
  • The command is the right answer expected that i recommend using /B switch. Without that switch an extrat byte 0x1A is added at the end of the resulting file. No matter the csv files, it tells copy to perform a binary copy which avoid any alteration during the copy. – Shenron May 07 '23 at 13:47
26
copy /b file1 + file2 + file3 newfile

Each source file must be added to the copy command with a +, and the last filename listed will be where the concatenated data is copied to.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 1
    there isnt a copy all (*) command? BC i have like 30 to 40 files. – edmon Jul 20 '11 at 15:45
  • 1
    This version lets you specify the exact order in which you'd like the files copied. with the *.csv method above, you'll get the files in random-ish order. – Marc B Jul 20 '11 at 18:19
5

If this is part of a batch script (.bat file) and you have a large list of files, you can use a multi-line ^, and optional /Y flag to suppresses prompting to confirm you want to overwrite an existing destination file.

REM Concatenate several files to one
COPY /Y ^
    this_is_file_1.csv + ^
    this_is_file_2.csv + ^
    this_is_file_3.csv + ^
    this_is_file_4.csv + ^
    this_is_file_5.csv + ^
    this_is_file_6.csv + ^
    this_is_file_7.csv + ^
    this_is_file_8.csv + ^
    this_is_file_9.csv ^
        output_file.csv

This is tidier than performing the command on one line.

Mike T
  • 41,085
  • 18
  • 152
  • 203
  • This format is much cleaner. And I can easily format and prepare thousands of file names in VS Code. Thanks! – jinhr Aug 13 '19 at 14:59
  • this doesn't work in DOS (as this question is tagged) because DOS doesn't support `^` as an escape character – phuclv Dec 14 '19 at 10:30
  • @phuclv, windows batch does use ^ for splitting lines in batch scripts, see here https://stackoverflow.com/questions/69068/split-long-commands-in-multiple-lines-through-windows-batch-file – default_avatar Apr 12 '21 at 09:13
  • @default_avatar of course I know that, which is why I commented as above. And it's **not** the character for splitting lines but the [escape character](https://en.wikipedia.org/wiki/Escape_character#Windows_Command_Prompt) in Windows cmd. This question is tagged MS DOS so this doesn't apply. DOS doesn't have any escape character and commands must be in a single line – phuclv Apr 12 '21 at 10:41
4
type data1.csv > combined.csv
type data2.csv >> combined.csv
type data3.csv >> combined.csv
type data4.csv >> combined.csv

etc.

Assume that your using files without headers and all files have the same columns.

Codebeat
  • 6,501
  • 6
  • 57
  • 99
4

filenames must sort correctly to combine correctly!

file1.bin file2.bin ... file10.bin wont work properly

file01.bin file02.bin ... file10.bin will work properly

c:>for %i in (file*.bin) do type %i >> onebinary.bin

Works for ascii or binary files.

Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
1
for %f in (filenamewildcard0, filenamewildcard1, ...) do echo %f >> newtargetfilename_with_path

Same idea as Mike T; might work better under MessyDog's 127 character command line limit

-3

make sure you have mapped the y: drive, or copy all the files to local dir c:/local

c:/local> copy *.* c:/newfile.txt

Ajay
  • 7
  • @AnnaLear The same mysterious `y` drive... where it's something strange... in the neighbourhood. Who you gonna call? https://www.youtube.com/watch?v=BMPcuZZgmtE – rayryeng Aug 05 '14 at 21:30