2

I have a set of 300 chapters named Chapter 1.txt, Chapter 2.txt... Chapter 300.txt]

The goal is to try to create a combined .txt file with all of these chapters in order.

Each file is formatted with the first line being the Chapter Title and the remainder of the file being the content of the chapter.

I tried

copy *.txt newfile.txt

This generated newfile.txt with two issues.

Issue 1 - The files were arranged in the order Chapter 1, Chapter 10, Chapter 100, Chapter 11... as supposed to Chapter 1, Chapter 2, Chapter 3...

Issue 2 - As mentioned, the first line of each file is the title. When these files were merged, the last line of the previous chapter is placed on the same line as the chapter title for the next chapter.

How would I fix the above?

1
  • 2
    Could be possible using a for loop counting from 1 to 300 used for generating the file name. In this loop append the file content to a fixed file, followed by a new-line.
    – Robert
    Jul 27, 2021 at 4:25

2 Answers 2

1
@echo off

cd /d "%~dp0" && set "_c=10000" && setlocal enabledelayedexpansion

for /l %%L in (1 1 300)do for /f tokens^=1* %%i in ('%__AppDir__%where.exe .:Chapter?%%~L.txt')do (
    set /a "_c+=1" && call set "_new=Chapter !_c:~-4!.txt" && rename ".\Chapter?%%~L.txt" "!_new!")

for /f tokens^=* %%i in ('%__AppDir__%where.exe .:Chapter*.txt')do echo;>>"%%~dpnxi"
copy ".\Chapter*.txt" ".\newfile.txt" & endlocal 

  • You can .\rename your files to an order that meets an alphanumeric, not just numeric, sequence.

1. Replace with the full path of the folder where you have your files.

cd /d "%~dp0" cd /d  "D:\Full\Path\To\Folder

2. Use a loop to get the order in which you go from 1 to 300 (1 in 1 incrementing by 1).

for /l %%L in (1 1 300)do ...

3. A pre-defined variable with 2 extra digits to give us the 1 or more lead zeros, incrementing inside the loop to use when renaming, only the last 4 digits.

set "_c=10000" 
set /a "_c+=1" && call set "_new=Chapter !_c:~-4!.txt"

4. An additional for /F loop will list each file in the correct order obeying the for /L loop also in order 1 to 300.

for /f tokens^=1* %%i in ('%__AppDir__%where.exe .:Chapter?%%~L.txt')do 

5. With the variable incremented in the loop, use substring to rename your files m led by zeros

:: Original Name    <==>   New File Name
:: -----------------------------------------
:: Chapter 1.txt    <==>   Chapter 0001.txt
:: Chapter 10.txt   <==>   Chapter 0010.txt
:: Chapter 100.txt  <==>   Chapter 0100.txt

set "_c=10000"
 
set /a "_c+=1" && call set "_new=Chapter !_c:~-4!.txt"

rename ".\Chapter?%%~L.txt" "!_new!"

6. When the executions of the double loop are finished, execute use echo to add the extra line at end of each of your files...

for /f tokens^=* %%i in ('%__AppDir__%where.exe .:Chapter*.txt')do echo;>>"%%~dpnxi"

7. After all the above processing, you are ready to execute your command and get the desired result:

copy ".\Chapter*.txt" ".\newfile.txt"

  • Obs.: I don't know what encode your files are, but I suppose a /binary copy doesn't overwrite the last line.
@echo off

cd /d "%~dp0" && set "_c=10000" && setlocal enabledelayedexpansion

for /l %%L in (1 1 300)do for /f tokens^=1* %%i in ('%__AppDir__%where.exe .:Chapter?%%~L.txt')do (
    set /a "_c+=1" && call set "_new=Chapter !_c:~-4!.txt" && rename ".\Chapter?%%~L.txt" "!_new!")

copy /b ".\Chapter*.txt" ".\newfile.txt" & endlocal 

Additional Resources:

2
  • Interesting you renamed the files, my intention was to type each file and append the output using >> to the final file. Then you could do everything in the for loop.
    – Robert
    Jul 27, 2021 at 13:45
  • @Robert I don't know the size of the files and maybe it would take more time... there are 300, how many lines?
    – Io-oI
    Jul 27, 2021 at 13:50
2

Copy the batch to the folder where the txt files are and execute:

@echo off

:: Set the name of the new concatenated file here:
set NewFile=NewFile.txt

If not exist "%NewFile%" copy NUL "%NewFile%"

for /L %%a in (1,1,300) do If exist "%Chapter %%a.txt" copy "%NewFile%" + "Chapter %%a.txt"& echo.>>"%NewFile%" &echo.>>"%NewFile%"
3
  • Instead of copy "%NewFile%" + "%%a.txt"& echo.>>"%NewFile%" it should be possible to directly use type "%NewFile%" + "%%a.txt" >> "%NewFile%"
    – Robert
    Jul 27, 2021 at 14:28
  • @Robert: The type command doesn't work that way... Jul 27, 2021 at 14:36
  • That could work: type %%a.txt>>"%NewFile%"& echo.>>"%NewFile%" Jul 27, 2021 at 14:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .