I was trying to print out an ebook today, but the book had over 30 chapters with each chapter in a separate text file named chapter##.txt.
\Book XYZ\ chapter01.txt chapter02.txt chapter03.txt ... chapter35.txt
I didn't want to print all the files separately and renumber the pages each time, so I used some stupid DOS tricks to combine them into one text file, with the beginning of each chapter clearly indicated by a blank line and its chapter number. There are a lot of full-fledged software tools that can achieve this, Python for example, but the easiest is actually DOS if you are running Windows. No special software needed, no programming skill required, just type your code at the DOS prompt and you'll get your concatenated text file in a few seconds.
Here is the step-by-step instruction with explanations:
1. Open a DOS window. ( If you are using Windows XP, go to Start menu, select Run, then type cmd )
2. Go to the directory that contains your text files using the cd command.
3. Enter the following code:
for %f in (chapter*.txt) do type "%f" >> mybook.txt
For each file named chapter##.txt in the directory, it would append the file content to the end of mybook.txt. The "for" command batch processes all text files that meet the criteria. The "type" command outputs the content of the file, and the ">>" operator appends the content to the end of mybook.txt.
We got the combined text file except the chapters are placed in the file back to back without any visual separators. Next we will improve our code and add chapter headings:
for %f in (chapter*.txt) do dir /B "%f" >>mybook.txt & type "%f" >> mybook.txt
The dir /B "%f" command lists file names without its size or summary information, and the "&" operator tells DOS to perform multiple commands sequentially. We are almost there except that we would want to add blank lines above and below the chapter headings, and a string of *********** at the beginning of each chapter to make the separation stand out more. Here is the final code:
for %f in (chapter*.txt) do echo+ >> mybook.txt & echo ****************************** >> mybook.txt & echo+ >>mybook.txt & dir /B "%f" >>mybook.txt & echo+ >>mybook.txt & type "%f" >> mybook.txt
The "echo" command outputs the text string after it, and "echo+" simply means to output a blank line. The final result should look something like this:
******************************
Chapter01.txt
Chapter 01 content...
******************************
Chapter02.txt
Chapter 02 content
******************************
Chapter03.txt
Chapter 03 content


Recent Comments