Wednesday, March 30, 2011

Join Multiple Text Files using Dirty Operating System (DOS)


Dirty Operating System (DOS)

Few days back I needed to join few (Thousand) text file as One file. I thought of
several options

  1. Open Notepad and copy paste files one by one :(
  2. Write a program which joins files.
  3. Download and use 3rd party program like TXTCollector
  4. Try something new ...

I remembered earlier (dark) days of DOS (Dirty Operation System) when screen was
black and text was white, everything was between 32 rows and 80 columns. Few of
DOS commands were very interesting like | (more) and > or >> (pipe). I thought to
do an experiment with that. I went to DOS prompt in same directory where all .txt
files were stored and used following command

type *.txt > MergedFile.txt

and ...

Yes !!! I got combined file named MergetFile.txt which was 14GB. There are few more
tricks like this

type firstFile.txt secondFile.txt thirdFile.txt fifthFile.txt > MergedFile.txt

This will join FirstFile.txt, SecondFile.txt, ThirdFile.txt and fifthFile.txt into
MergedFile.txt, this is useful when you want to join files in certain order.

Please note that each file is separated by a space. As you may already know, if
are in different folders please use full path instead of just file name.

type *.txt *.csv >MergedFile.txt

This will first join all files with .txt extension and then will join all files
with .csv extension to create a file called MergedFile.txt Here the magic command
is > which takes output of left side command and gives as input to right side command.
If used as >> then output gets be appended. As stated in
the principal of "Occam's razor" "The simplest explanation is
most likely the correct one"

Instead of writing my own code (spending few hours) or download or buy 3rd party
tool (spending money earned in few hours !!!) I used DOS to do the work. Hope this
helps Enjoy coding !!!