SOLVED
Home

Combining Text Files Into Single CSV

%3CLINGO-SUB%20id%3D%22lingo-sub-219726%22%20slang%3D%22en-US%22%3ECombining%20Text%20Files%20Into%20Single%20CSV%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-219726%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%20I%20have%20a%20business%20situation%20where%26nbsp%3BI%20have%20to%20combine%20several%26nbsp%3BTXT%20files%20into%20one%20CSV.%20I'm%20looking%20for%20a%20way%20to%20automate%20the%20process%20of%20combining%20them%20as%20much%20as%20possible%2C%20even%20if%20it%20means%20a%203rd%20party%20batch%20file%20or%20something.%26nbsp%3BThe%20text%20files%20are%20essentially%20already%20csv%20files%20but%20someone%20is%20saving%20them%20wrong%20and%20there's%20nothing%20I%20can%20do%20about%20that.%20I'm%20using%20Office%2016.%20The%20details%20are%20important%20so%20here%20they%20are%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20you%20open%26nbsp%3Beach%20text%20file%20with%20Excel%2C%20they%20all%26nbsp%3Bhave%20the%20same%20columns%20and%20first%20row%2C%20which%20is%20a%20header.%20It%20looks%20something%20like%3A%3C%2FP%3E%3CP%3Eimportant%23%20%7C%20date%20%7C%20etc%3C%2FP%3E%3CP%3E123%20%7C%2012%2F25%2F18%20%7C%20etc%3C%2FP%3E%3CP%3E345%26nbsp%3B%7C%2012%2F25%2F18%20%7C%20etc%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUsually%20what%20I%20do%20is%20open%20the%20first%20text%20file%20up%20in%20Excel%20using%20the%20%22Open%20With%22%20right-click%26nbsp%3Boption%2C%20then%20%22Save%20As%22%20a%20CSV%20with%20a%20new%20name%2C%20then%20individually%20open%20each%20file%20and%20copy%2Fpaste%20the%20rows%20with%20content%26nbsp%3Bone%20after%20another%20into%20the%20first%20one.%20The%20result%20is%20a%20CSV%20with%20all%20the%20rows%20filled%20out%20and%20one%20header%20row.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDoes%20anyone%20know%20of%20a%20way%20to%20combine%20these%20files%20together%20into%20one%20CSV%20(for%20example%2C%20all%20the%20files%20in%20a%20given%20directory)%20with%20just%20a%20few%20clicks%3F%20I%20presume%20this%26nbsp%3Bwould%20involve%20scripting%20but%20maybe%20someone%20has%20already%20written%20such%20a%20thing%20and%20would%20be%20willing%20to%20share%20it%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%26nbsp%3Bhave%20no%20idea%20how%20to%20write%20such%20a%20script%2C%20but%20I%20know%20what%20it%20would%20do%3A%20for%20a%20given%20group%20of%20files%2C%20pull%20the%20data%20from%20all%20rows%20on%20the%20first%20one%20and%20put%20it%20in%26nbsp%3Ba%20new%20Excel%20file%2C%20then%20for%20all%20files%20after%2C%20pull%20the%20data%20below%20row%201%20and%20append%20it%20to%20the%20first%20one.%20Would%20anyone%20be%20kind%20enough%20to%20have%20a%20crack%20at%20it%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20attaching%20a%26nbsp%3Bsample%20text%20file%20for%20example%2C%20however%20I%20had%20to%20change%20it%20to%20a%20CSV%20in%20order%20for%20the%20forum%20to%20allow%20me%20to%20upload%20it.%20Also%20the%20data%20is%20changed%20for%20privacy.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20your%20help!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E(edited%26nbsp%3Bto%20add%20version%20info)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-219726%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-220180%22%20slang%3D%22en-US%22%3ERe%3A%20Combining%20Text%20Files%20Into%20Single%20CSV%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-220180%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Sergei%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20that%2C%20I'm%20on%20PC%20and%20Power%20Query%26nbsp%3Bdoes%20pretty%20much%20what%20I%20need%20as%20it%20turns%20out.%20Much%20simpler%20than%20I%20imagined.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20again%20for%20your%20help!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-219761%22%20slang%3D%22en-US%22%3ERe%3A%20Combining%20Text%20Files%20Into%20Single%20CSV%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-219761%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Nathan%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAre%20you%20on%20Windows%20or%20on%20Mac%3F%3C%2FP%3E%0A%3CP%3EAssuming%20all%20you%20csv%20file%20are%20in%20one%20folder%20for%20Windows%20you%20may%20use%20command%20line%20like%3C%2FP%3E%0A%3CPRE%3Ecopy%20*.csv%20output.csv%3C%2FPRE%3E%0A%3CP%3Eto%20combine%20all%20files.%20The%20only%20point%20first%20row%20of%20each%20file%20will%20be%20kept%20within%20the%20resulting%20file%2C%20but%20most%20probably%20you%20may%20find%20more%20complicated%20command%20to%20resolve%20that.%3C%2FP%3E%0A%3CP%3EI%20believe%20something%20similar%20shall%20be%20for%20Mac%2C%20that%20is%20Unix%20after%20all.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20Windows%20you%20may%20use%20Power%20Query%20to%20combine%20and%20transform%2C%20if%20necessary%2C%20all%20your%20files%20in%20one%20resulting%20Excel%20table%2C%20copy%20of%20that%20resulting%20file%20you%20may%20save%20as%20csv.%20See%2C%20for%20example%2C%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fwww.myonlinetraininghub.com%2Fpower-query-get-files-from-a-folder%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.myonlinetraininghub.com%2Fpower-query-get-files-from-a-folder%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENot%20sure%20if%20Power%20Query%20is%20available%20for%20Mac%20now.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Nathan Wendel
New Contributor

Hi, I have a business situation where I have to combine several TXT files into one CSV. I'm looking for a way to automate the process of combining them as much as possible, even if it means a 3rd party batch file or something. The text files are essentially already csv files but someone is saving them wrong and there's nothing I can do about that. I'm using Office 16. The details are important so here they are:

 

When you open each text file with Excel, they all have the same columns and first row, which is a header. It looks something like:

important# | date | etc

123 | 12/25/18 | etc

345 | 12/25/18 | etc

 

Usually what I do is open the first text file up in Excel using the "Open With" right-click option, then "Save As" a CSV with a new name, then individually open each file and copy/paste the rows with content one after another into the first one. The result is a CSV with all the rows filled out and one header row.

 

Does anyone know of a way to combine these files together into one CSV (for example, all the files in a given directory) with just a few clicks? I presume this would involve scripting but maybe someone has already written such a thing and would be willing to share it?

 

I have no idea how to write such a script, but I know what it would do: for a given group of files, pull the data from all rows on the first one and put it in a new Excel file, then for all files after, pull the data below row 1 and append it to the first one. Would anyone be kind enough to have a crack at it? 

 

I'm attaching a sample text file for example, however I had to change it to a CSV in order for the forum to allow me to upload it. Also the data is changed for privacy.

 

Thank you for your help!

 

(edited to add version info)

2 Replies
Solution

Hi Nathan,

 

Are you on Windows or on Mac?

Assuming all you csv file are in one folder for Windows you may use command line like

copy *.csv output.csv

to combine all files. The only point first row of each file will be kept within the resulting file, but most probably you may find more complicated command to resolve that.

I believe something similar shall be for Mac, that is Unix after all.

 

For Windows you may use Power Query to combine and transform, if necessary, all your files in one resulting Excel table, copy of that resulting file you may save as csv. See, for example, https://www.myonlinetraininghub.com/power-query-get-files-from-a-folder.

 

Not sure if Power Query is available for Mac now.

Hi Sergei,

 

Thank you for that, I'm on PC and Power Query does pretty much what I need as it turns out. Much simpler than I imagined.

 

Thanks again for your help!

Related Conversations
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
28 Replies
Tabs and Dark Mode
cjc2112 in Discussions on
2 Replies
Early preview of Microsoft Edge group policies
Sean Lyndersay in Discussions on
65 Replies
*Updated 9/3* Syncing in Microsoft Edge Preview Channels
Elliot Kirk in Articles on
204 Replies