我需要创建一些.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有什么方法可以从每个文件名中“提取”文本并将其添加到“: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
我可能会使用以下脚本(让我们称之为extract-chapter-numbers.bat
)完成您的任务。这期望将章节部分(c
,后跟数字)作为.zip
文件名中的最后一个或倒数第二个以空格分隔的标记;如果它是倒数第二个,则最后一个应该是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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句