How to batch merge multiple .TXT Files into one New .TXT File?

2.6k Views Asked by At

I would like to automate the publishing of articles in wordpress for my website. I have like the following files:

Is this already the solution in windows CMD?

C:\>For %f in (in*.txt) do type %f >> Combined.txt

(A FOLDER WITH 20.000 Titles)

  • Title_1
  • Title_2
  • Title_3
  • Title_4
  • Title_5

(AND A FOLDER WITH 20.000 ARTICLES)

  • Article_1
  • Article_2
  • Article_3
  • Article_4
  • Article_5

The numbers are associated with the respective file. That means title 1 belongs to article 1 and so on.

How can I automatically merge the title and the article number by number?

1

There are 1 best solutions below

0
Drake Johnson On

There are certainly multiple ways to do this in CMD. The first way that comes to mind is to have a for-loop that runs for the total number of files. On every run, append the contents of the title file to a "merged" file, then append the contents of the articles file to that same file. Also, I'd recommend you have a file structure that looks like:

      Parent Folder
            |
   |------------------|
   |                  |
Titles             Articles

In other words, have your folder with your titles and your folder with your articles sub-directories of the same folder. This will allow you to save this script in "Parent Folder," which will make directory browsing easier to follow.

@echo off
echo Starting...
setlocal EnableDelayedExpansion

REM Save the directory of "Parent Folder"
set parent_dir=!cd!
if not exist Merged md Merged

REM Initialize other directories
set titles_dir=!parent_dir!\Titles
set articles_dir=!parent_dir!\Articles
set merged_dir=!parent_dir!\Merged

REM However many files you have goes below
set NUMBER_OF_FILES=100
for /l %%a in (1, 1, !NUMBER_OF_FILES!) do (
    for /f "tokens=*" %%b in (!titles_dir!\Title_%%a.txt) do (
        echo.%%b>>!merged_dir!\Merged_%%a.txt
    )
    for /f "tokens=*" %%b in (!articles_dir!\Article_%%a.txt) do (
        echo.%%b>>!merged_dir!\Merged_%%a.txt
    )
)
echo Done
@echo on

And that's it. You should have fully merged titles and articles. I tested this with 100 generated text files. Here is my script for the test (the script was also saved the "Parent Folder" directory):

@echo off
echo Starting...
setlocal EnableDelayedExpansion
if not exist Titles md Titles
if not exist Articles md Articles

set parent_dir=!cd!
set titles_dir=!parent_dir!\Titles
set articles_dir=!parent_dir!\Articles

set NUMBER_OF_FILES=100
for /l %%a in (1, 1, !NUMBER_OF_FILES!) do (
    echo Title%%a>!titles_dir!\Title_%%a.txt
    echo ----->>!titles_dir!\Title_%%a.txt
    echo. >>!titles_dir!\Title_%%a.txt
    for /l %%b in (1, 1, 100) do (
        echo This is article number %%a>>!articles_dir!\Article_%%a.txt
    )
)
echo Done
@echo on

Just a quick note: This may be worth doing in a faster language, like C++, especially if you're working with a very large number of files.


Edit
I tested this script and was getting a little over 550 files/second generated per second from my initial test script that generated the Title and Article files.