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

时间:2019-01-25 13:20:20

标签: batch-file text-files edit

我需要创建一些.cbc文件以与Calibre一起使用。它们基本上被重命名为.zip文件,其中每个章都包含在另一个.zip和一个名为comics.txt的文本文件中,该文本文件指向每个文件名并为其指定目录名称。

我已经使用了一堆.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 

2 个答案:

答案 0 :(得分:2)

唯一的挑战是找到章节编号。它始终以<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

答案 1 :(得分:1)

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

下面是代码:

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