.bat文件,它将文本附加到.txt的每一行中,包括该行本身的一部分

花栗鼠

我需要创建一些.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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用该行的一部分索引到file2并查找值后,如何在该行的每一行附加值?

在python中读取文件时如何跳过每一行的一部分

如何在行尾添加一行的一部分(对于文件中的每一行)

python-从文本文件中提取每一行的一部分

删除文本文件每一行的第一部分

从一个文件复制一行的一部分,并使用额外的文本附加到另一个文件

如何从目录中所有.txt文件中获取行的一部分?

在Shell脚本中读取文件并选择该行的一部分

如何读取文本文件中一行的一部分?

查找一行中的一部分文本,然后将其添加到同一行的另一部分中

获取行的一部分并追加到同一行

使用sed从文件中提取一行的一部分

修改文件中一行的一部分

如何只读取大文件中一行的一部分?

如何读取txt文件的一部分

打印.txt文件的一部分

如何在ruby中用regex和.sub覆盖txt文件中的行的一部分

为什么用内置int()读取.txt文件中的行的一部分抛出ValueError?

从文本中获取一行的一部分作为字符串

在python的文本文件中替换特定行的一部分

如何在Java中仅下载txt文件的一部分?

查找并替换txt文件中的一部分字符串

附加到一部分结果

外部文件中的WHILE的一部分

查找文件的一行,提取其中的一部分并替换在 Bash 中的同一位置

当我用Python写入CSV文件时,它将复制最后一行的一部分并将其写入文件的底部

在python中獲取不是第一部分或不存在於另一個文件行中的txt文件行

在Perl中读取CSV文件中的行的一部分

有没有办法只读取输入文件的一行的一部分?