.bat 文件,它在 .txt 的每一行附加文本,包括行本身的一部分

.bat file which appends text at each line in a .txt, including part of the line itself

我需要创建一些 .cbc 文件以用于 Calibre。它们基本上被重命名为 .zip 文件,其中包含另一个 .zip 中的每一章和一个名为 comics.txt 的文本文件,该文件指向每个文件名并为其提供 TOC 的章节名称。

我已经用一堆 .bat 文件自动化了大部分过程。但是有一个烦恼我不知道如何开始修复。

文本文件的每一行都需要以下格式:

[Filename of zip containing chapter].zip:Chapter xxx

例如:

Serie name c015 v03.zip:Chapter 15
Serie name c016 v03.zip:Chapter 16
Serie name c016.1 v03.zip:Chapter 16.1

对于流程的这个特定部分,我使用以下 .bat:

dir /b *.zip > comic.txt
(for /f "delims=" %%i in (comic.txt) do @echo %%i:Chapter )>comics.txt
del comic.txt
start comics.txt

这会将 :Chapter 添加到每行的末尾,从而节省了我的部分工作。但是,我还是需要手动添加章节名,所以我在最后直接打开文件。

.bat 是否可以从每个文件名中提取 "take" 文本并将其添加到“:Chapter”之后,而不是获取

Serie name c015 v03.zip:Chapter 
Serie name c016 v03.zip:Chapter 
Serie name c016.1 v03.zip:Chapter  

我直接得到:

Serie name c015 v03.zip:Chapter 15 
Serie name c016 v03.zip:Chapter 16 
Serie name c016.1 v03.zip:Chapter 16.1 

唯一的挑战是找到章节编号。它总是以 <space>c

开头
@echo off
setlocal enabledelayedexpansion

REM following two lines to create test environment:
for /l %%a in (14,1,20) do break>"The Hulk The Beginning c0%%a v03.zip"
break>"The Hulk The Beginning c016.1 v03.zip"
(
REM for every .zip file:
for %%a in (*.zip) do (
  for %%b in (%%~na) do ( 
   REM find a token which starts with 'c' and at least one number:
   echo %%b|findstr /b "c[0-9][0-9]*" >nul && set chapter=%%b
  )
  echo %%~nxa:Chapter !chapter:~1!
))>comics.txt

我可能会使用以下脚本完成您的任务(让我们称之为 extract-chapter-numbers.bat)。这期望章节部分(c,后跟一些数字)作为 .zip 文件名中的最后一个或倒数第二个 space 分隔的标记;如果它是倒数第二个,则最后一个应该是 v,后跟一些数字(如果该部分可以是任何内容,只需将脚本中的行 set "_FILTER_V=v[0-9\.][0-9\.]*" 替换为 set "_FILTER_V=.*").

代码如下:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_ROOT=."                    & rem // (directory containing the files to process)
set "_FILTER_C=c[0-9\.][0-9\.]*" & rem // (`findstr` expression for the chapter part)
set "_FILTER_V=v[0-9\.][0-9\.]*" & rem // (`findstr` expression for the version part)
set "_FILTER_E=zip"              & rem // (`findstr` expression for the file extension)
set "_PREFIX=Chapter"            & rem // (string to precede the chapter number with)

rem /* Loop through all matching files that contain a chapter number (preceded by `c`)
rem    and an optional version code preceded by `v`): */
for /F delims^=^ eol^= %%J in ('
    dir /B /A:-D "%_ROOT%\* c*.%_FILTER_E%" ^| findstr /I /R /E ^
        /C:"..*  *%_FILTER_C%  *%_FILTER_V%\.%_FILTER_E%" ^
        /C:"..*  *%_FILTER_C%\.%_FILTER_E%" ^
') do (
    rem // Store current file name, reset some variables:
    set "LINE=%%J" & set "LAST=" & set "NEXT="
    rem /* Loop through all space-separated parts of the base file name
    rem    (note that `,`, `;`, `=` are also treated as separators): */
    for %%I in (%%~nJ) do (
        rem // Store the last and the next-to-last parts:
        call set "NEXT=%%LAST%%" & set "LAST=%%I"
    )
    setlocal EnableDelayedExpansion
    rem /* Check whether the last or the next-to-last part begins with `c`, and if so,
    rem    extract the following numeric part: */
    if /I "!LAST:~,1!"=="c" (
        set "CHPT=!LAST:~1!"
    ) else if /I "!NEXT:~,1!"=="c" (
        set "CHPT=!NEXT:~1!"
    ) else (
        set "CHPT="
    )
    rem // Return the original file name, together with a prefix and chapter number:
    if defined CHPT echo(!LINE!:!_PREFIX! !CHPT!
    endlocal
)

endlocal
exit /B

如果您 运行 它来自包含您的示例文件的目录:

Serie name c015 v03.zip
Serie name c016 v03.zip
Serie name c016.1 v03.zip

输出将是:

Serie name c015 v03.zip:Chapter 015
Serie name c016 v03.zip:Chapter 016
Serie name c016.1 v03.zip:Chapter 016.1

要将其存储在文本文件中(比如 comics.txt),运行 脚本如下:

extract-chapter-numbers.bat > comics.txt