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?
This question is related to
dos
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.
make sure you have mapped the y: drive, or copy all the files to local dir c:/local
c:/local> copy *.* c:/newfile.txt
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.
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.
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
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.
Source: Stackoverflow.com