Tuesday, February 11, 2014

Using Windows command prompt to merge text files for later use

I'm super-busy getting ready to defend my dissertation, but I'm getting a lot of good material saved up from the process (I've gotten a lot of practice with loops lately).  Meanwhile, here's a quick trick I learned when desperately hoping I wouldn't have to concatenate 240 text files by hand.  (Spoiler: I didn't!)

First, gather up your text files into one folder.  I did a search in the folder where mine were stored in numerous subfolders using *spacing*.txt to get all .txt files with "spacing" in the name.  This got me my 240 files.  I copied them into a new folder in case something went wrong and to get them all into one place for the next step.

Open up the Windows command prompt (cmd.exe).  Navigate to the folder where you are storing all of your text files using the cd command.  I had my desired folder open, copied the folder address, and pasted it into the command prompt.  I had to right-click to paste (ctrl-v didn't work).  Here is the command prompt when I've pasted into the folder address.  "cd" is needed before your address.







 Press enter, and then we've navigated to the desired folder:






Now, input your command:


copy /b *.txt merged.txt

The purpose of  the /b stumped for me for a while (it was just in the original instructions that I found), but it appears that it specifies a binary file.  You can find more info on variations on copy that might suit your needs here.

You should end up with all the data from your original files (which should be still intact in that folder) into a single new text file named "merged.txt".  Hours of copy/paste saved!  Yay!  (I was happy, for sure.)

In my case, every single text file already had a heading, so I opened the new merged file in LibreOffice Calc (Microsoft Excel or any other spreadsheet does just as well) and did a variety of sorts to clean up the new file and delete most of those heading rows.  Then my file was ready for other uses!

2015/11/06 Edited to add: Now that I have a computer with a partitioned hard drive, I discovered I couldn't use cd to get to my E:/ drive.  A quick search indicated I just need to type the E: and press enter, then proceed as above.

No comments:

Post a Comment

Comments and suggestions welcome.