0

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?

Corentin Limier
  • 4,946
  • 1
  • 13
  • 24
  • 1
    The Windows command line `for %I in ("Folder with Titles\Title_*") do @for /F "tokens=2 delims=_" %J in ("%~nI") do copy /Y /B "%I" + "Folder with Articles\Article_%J.ext" "Folder with Merged Files\Merged_%J.ext" >nul` could produce the files you would like to get by this command line. Run in [command prompt](https://www.howtogeek.com/235101/) `for /?` and read the output help carefully from top of first page to bottom of last page and next `copy /?` and read also the output help for an explanation of this command line. – Mofi Jan 27 '20 at 06:52
  • @Mofi Thank you soooooo much, this did exactly what i wanted. It is working perfectly fine! Thank you very much !!! – Julian Fleischer Jan 27 '20 at 11:29

1 Answers1

0

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.

Drake Johnson
  • 640
  • 3
  • 19