Windows批处理文件-在文件中查找字符串并将其追加

时间:2018-12-04 01:39:52

标签: string windows file batch-file append

所以我非常接近我想要达到的目标,但是我似乎无法超越我目前坚持的观点。

首先,我有一个.ini文件,该文件的标头以及用逗号分隔的项目列表:

[Items]
ListOfItems = Item01, Item02, Item03

我需要做的是找到特定部分( ListOfItems ),并向其添加参考文件中不存在的新项目。

这是我到目前为止的内容:

SET /P NewItem=
SET ListOfItems=
FOR /F delims^=^"^ tokens^=2 %%G IN ('type File.ini') DO  (set ListOfItems=%%G)
echo ListOfItems ^=^"%ListOfItems%, %NewItem%^">File.ini

我遇到的问题是我使用提示而不是从文件( List.txt )中获取新行,因为我上次尝试不断重复此行,而不是只重复一行,只要不是重复行即可。

我遇到的另一个问题是,使用上面的当前代码,它不会保留.ini文件的当前状态。因此,您可以按照上面的示例操作,但是一旦通过提示符添加新项目,您最终会得到:

ListOfItems =", Item04"

标头不见了,原始值不见了,它以逗号开头,而不是仅在每个值后面添加逗号。

所以预期结果是:

[Items]
ListOfItems = Item01, Item02, Item03, Item04

为什么第一次运行时不保留原始数据,但随后的运行会完美地复制原始数据并添加到原始数据中?

我们如何解决逗号开头的问题,而前面没有数据?

如何让它从 List.txt 中提取要追加的项目列表,而不是一次手动输入一项?

编辑:我设法解决了第二个问题:

(
    ECHO [Items]
    ECHO !ArchiveList! = !RequiredItems!
)>>File.ini

根据约翰·肯斯(John Kens)的建议,在!RequiredItems!包含Item01, Item02, Item03的基础项目中,我没有填写ListOfItems时所做的工作。

关于第三个问题,我用以下方法解决了它:

FOR /F "delims=" %%A IN (List.txt) DO (
    SET "ListAddition=!ListAddition!%%A, "
)
SET "ListAddition=!ListAddition:~0,-2!"

哪个将 List.txt 文件中的每个项目都放在新行上,然后将其添加到列表中,每个项目之间用逗号分隔。

1 个答案:

答案 0 :(得分:1)

首先,首先,您当前的for loop需要进行一些调整;我们可以在这里做一些事情。如果我们只是想从第二行中获取 ListOfItems 等于什么,而忽略了该文件中可能还有其他对象的事实,那么获取此数据的正确方法如下:

FOR /F "skip=1 tokens=2,*" %%A IN ('type File.ini') DO (set "ListOfItems=%%B")
  • skip=1 -将跳过第一行
  • tokens=2,* -具有 2 ** 将导致%%A是前两个对象,而%%B将是之后的所有对象2。

但是,更恰当地说,正确的方法是使用find /i语句在.ini文件中查找 ListOfItems 对象。让我们来看下面的文本文件:

[Items]
ListOfItems = Item01, Item02, Item03
[Armor]
ListOfArmor = Iron Helmet, Iron Brestplate, Iron Pants
[Weapons]
ListOfWeapons = Sword, Dagger, Holy Water

如果在此语句上使用基本的for循环,则将获取文本文件的最后一行:

Sword, Dagger, Holy Water

Bellow正在使用find语句以及另一个循环,我们可以将它们组合在一起,以仅从整个文档中提取 ListOfItems 之后的数据。

Rem | Get .ini To String
FOR /F "tokens=*" %%A IN ('type File.ini') DO (

    Rem | Look For Line With Items "ListOfItems"
    FOR /F "tokens=2,*" %%B IN ('echo %%A^| find /i "ListOfItems"') DO (

        Rem | Echo Result
        echo %%C
    )
)

但是,仅纠正那一行并在末尾添加一个新对象,这很棘手!请记住,批处理是原始的且缺乏支持,与它的后继者powershell相比,它的局限性是有限的。在原始批处理中,没有真正的命令可以只编辑文档中间的一行。但是,这并非不可能。

要解决此问题,我们将必须获取整个.ini文件,并使用type命令逐行分解文档并将其保存为字符串。在这里,我们可以使用syntax-replace编辑“字符串”并将其保存到新文档中。从那里我们只删除并重命名。

要对此进行进一步扩展,我们将需要检查ListOfItems是否实际填充。基本的if exists语句在这里非常有用。现在,由于您的语句的等式中有一个=,因此简单的syntax-replace就不会产生进一步的麻烦。从我之前的编辑中,我将简单功能更改为脚本,我们也将call。该脚本将称为Replace.bat。您需要做的就是制作一个新的.bat文件并将其粘贴到波纹管中。此文件将永远不需要修改。

Bellow是应该解决您所有问题的整个项目:

替换。蝙蝠:

(整个脚本等效于powershell lol中的一个15个字符的命令!)

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "FILE_I=%~1"
set "SEARCH=%~2"
set "REPLAC=%~3"
set "FILE_O=%~4"
set "FLAG=%~5"
if not defined FILE_I exit /B 1
if not defined SEARCH exit /B 1
if not defined FILE_O set "FILE_O=con"
if defined FLAG set "FLAG=#"

for /F "delims=" %%L in ('
    findstr /N /R "^" "%FILE_I%" ^& break ^> "%FILE_O%"
') do (
    set "STRING=%%L"
    setlocal EnableDelayedExpansion
    set "STRING=!STRING:*:=!"
    call :REPL RETURN STRING SEARCH REPLAC %FLAG%
    >> "%FILE_O%" echo(!RETURN!
    endlocal
)

endlocal
exit /B


:REPL  rtn_string  ref_string  ref_search  ref_replac  flag
setlocal EnableDelayedExpansion
set "STR=!%~2!"
set "SCH=!%~3!"
set "RPL=!%~4!"
if not defined SCH endlocal & set "%~1=" & exit /B 1
set "SCH_CHR=!SCH:~,1!"
if not "%~5"=="" set "SCH_CHR="
if "!SCH_CHR!"=="=" set "SCH_CHR=" & rem = terminates search string
if "!SCH_CHR!"==""^" set "SCH_CHR=" & rem " could derange syntax
if "!SCH_CHR!"=="%%" set "SCH_CHR=" & rem % ends variable expansion
if "!SCH_CHR!"=="^!" set "SCH_CHR=" & rem ! ends variable expansion
call :LEN SCH_LEN SCH
call :LEN RPL_LEN RPL
set /A RED_LEN=SCH_LEN-1
set "RES="
:LOOP
call :LEN STR_LEN STR
if not defined STR goto :END
if defined SCH_CHR (
    set "WRK=!STR:*%SCH_CHR%=!"
    if "!WRK!"=="!STR!" (
        set "RES=!RES!!STR!"
        set "STR="
    ) else (
        call :LEN WRK_LEN WRK
        set /A DFF_LEN=STR_LEN-WRK_LEN-1,INC_LEN=DFF_LEN+1,MOR_LEN=DFF_LEN+SCH_LEN
        for /F "tokens=1,2,3 delims=," %%M in ("!DFF_LEN!,!INC_LEN!,!MOR_LEN!") do (
            rem set "RES=!RES!!STR:~,%%M!"
            if defined WRK set "WRK=!WRK:~,%RED_LEN%!"
            if "!STR:~%%M,1!!WRK!"=="!SCH!" (
                set "RES=!RES!!STR:~,%%M!!RPL!"
                set "STR=!STR:~%%O!"
            ) else (
                set "RES=!RES!!STR:~,%%N!"
                set "STR=!STR:~%%N!"
            )
        )
    )
) else (
    if "!STR:~,%SCH_LEN%!"=="!SCH!" (
        set "RES=!RES!!RPL!"
        set "STR=!STR:~%SCH_LEN%!"
    ) else (
        set "RES=!RES!!STR:~,1!"
        set "STR=!STR:~1!"
    )
)
goto :LOOP
:END
if defined RES (
    for /F delims^=^ eol^= %%S in ("!RES!") do (
        endlocal
        set "%~1=%%S"
    )
) else endlocal & set "%~1="
exit /B


:LEN  rtn_length  ref_string
setlocal EnableDelayedExpansion
set "STR=!%~2!"
if not defined STR (set /A LEN=0) else (set /A LEN=1)
for %%L in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
    if defined STR (
        set "INT=!STR:~%%L!"
        if not "!INT!"=="" set /A LEN+=%%L & set "STR=!INT!"
    )
)
endlocal & set "%~1=%LEN%"
exit /B

Main.Bat:

@ECHO OFF
@setlocal EnableDelayedExpansion

Rem | Configuration
set "CustomINI=File.ini"
set "ListHeader=[Items]"
set "Object=ListOfItems"
set "ReplaceScript=Replace.bat"
SET "ItemList=List.txt"

Rem | Check If "CustomINI" Exists
if not exist "%CustomINI%" (
    echo File "%CustomINI%" Not Found!
    pause
    goto :EOF
)

Rem | Check If "ItemList" Exists
if not exist "%ItemList%" (
    echo File "%ItemList%" Not Found!
    pause
    goto :EOF
)
goto StartFunction

:StartFunction

Rem | Generate the list of items from textfile
FOR /F "delims=" %%A IN (%ItemList%) DO (
    set "ListAddition=!ListAddition!%%A, "
)
set "ListAddition=!ListAddition:~0,-2!"

Rem | Get .ini To String
set HeaderFound=false
FOR /F "tokens=*" %%A IN ('type !CustomINI!') DO (

    Rem | First Find The Header "[Items]" & Extract "ListOfItems" Line Data
    for /f "tokens=*" %%B in ('echo %%A') do (
        set "item=%%B"
        if /i "!item!"=="!ListHeader!" (
            set HeaderFound=true
        ) else if not "!item!"=="!item:ListOfItems=!" if "!HeaderFound!"=="true" (

            Rem | Turn Items For Line "ListOfItems" To String
            for /f "tokens=2,*" %%C in ('echo %%B') do (

               Rem | Set String
               set "SEARCHTEXT=%%D"
            )
            set HeaderFound=false

        )
    )
)

Rem | Check If "ListOfItems" Is Actually Populated
If "%SEARCHTEXT%"=="" (
    Rem | Not Populated
    set "SEARCHTEXT=!Object! = "
    set "REPLACETEXT=!Object! = !ListAddition!"
    goto EditString
) ELSE (
    Rem | Populated
    set "REPLACETEXT=!SEARCHTEXT!, !ListAddition!"
    goto EditString
)

:EditString

Rem | Edit Only "ListOfItems" Line
Rem | Usage: call "1" "2" "3" "4" "5"
Rem | call - Calls external script
Rem | "1" - Name of External script
Rem | "2" - File to Edit
Rem | "3" - Text to replace ex: red apple
Rem | "4" - Text to replace to ex: green apple
Rem | "5" - Output file
call "%ReplaceScript%" "%CustomINI%" "%SEARCHTEXT%" "%REPLACETEXT%" "%CustomINI%.TEMP"

Rem | Delete Original File, Restore New
del "%CustomINI%"
rename "%CustomINI%.TEMP" "%CustomINI%"

goto :EOF

PS-请注意以下几点:上面的脚本期望当未填充ListOfItems =时,它在=后有一个空格。如果您的.ini文件中不是这样,请从set "SEARCHTEXT=!OBJECT! = "语句中将set "SEARCHTEXT=!OBJECT! ="更改为for


编辑:由于最近的请求,以下内容已更新:

首先,由于我不确定ListOfItems =的OP的含义是“空白”,因此我认为他/她指的是ListOfItems =。 -并非ListHeader本身真正丢失了它。在下面的示例中。

我的视野-File.ini:

[Items]
ListOfItems = 
[Armor]
ListOfArmor = Iron Helmet, Iron Brestplate, Iron Pants
[Weapons]
ListOfWeapons = Sword, Dagger, Holy Water

OP的愿景-File.ini

[Items]

[Armor]
ListOfArmor = Iron Helmet, Iron Brestplate, Iron Pants

[Weapons]
ListOfWeapons = Sword, Dagger, Holy Water

从那时起,我现在更新了脚本以查找[Items](字符串),然后在其下添加新行。这是由Magoo使用脚本完成的。

由于没有什么可替换的,我们只需将“添加”到.ini上,就可以调用另一个函数。

:EditMissingString

Rem | Export SearchString
echo !ListHeader!>> %~dp0ListHeader.txt

Rem | Add Text Under %ListHeader%
(
    FOR /f "delims=" %%i IN (ListHeader.txt) DO (
    SET AddAfter=%%i

    FOR /f "delims=" %%n IN ('findstr /n "^" %CustomINI%') DO (
        SET line=%%n
        SET line=!line:*:=!

        ECHO(!line!
            IF "!line!"=="!AddAfter!" ECHO(%AddTEXT%
        )
    )
)>>%CustomINI%.TEMP

Rem | Remove ListHeader.txt
DEL %~dp0ListHeader.txt

Rem | Delete Original File, Restore New
DEL %CustomINI%
REN %CustomINI%.TEMP %CustomINI%

goto :EOF

我们不再需要单独编辑ListOfArmor =,而是添加到它上面,因此我们不再需要 Replace.bat 脚本。我还修复了原始脚本,以正确保留空行!

  

新的替换功能,W / H线路保留。

:EditExistingString

REM | Make sure we only edit the ListOfItems line.
FOR /F "delims=" %%n IN ('findstr /n "^" %CustomINI%') DO (
    SET line=%%n
    SET Modified=!line:%SearchText%=%ReplaceText%!
    SET Modified=!Modified:*:=!

    REM | Output the entire edited INI to a temporary file.
    >> %CustomINI%.TEMP ECHO(!Modified!
)

Rem | Delete Original File, Restore New
DEL %CustomINI%
REN %CustomINI%.TEMP %CustomINI%

goto :EOF

结果:

[Items]
ListOfItems = Item1, Item2, Item3

[Armor]
ListOfArmor = Iron Helmet, Iron Brestplate, Iron Pants

[Weapons]
ListOfWeapons = Sword, Dagger, Holy Water

结果输出:

[Items]
ListOfItems = Item1, Item2, Item3, Item4, Item5, Item6

[Armor]
ListOfArmor = Iron Helmet, Iron Brestplate, Iron Pants

[Weapons]
ListOfWeapons = Sword, Dagger, Holy Water
  

最终批处理脚本:

@ECHO OFF
@setlocal EnableDelayedExpansion

Rem | Configuration
set "CustomINI=File.ini"
set "ListHeader=[Items]"
set "Object=ListOfItems"
SET "ItemList=List.txt"

Rem | Check If "CustomINI" Exists
if not exist "%CustomINI%" (
    echo File "%CustomINI%" Not Found!
    pause
    goto :EOF
)

Rem | Check If "ItemList" Exists
if not exist "%ItemList%" (
    echo File "%ItemList%" Not Found!
    pause
    goto :EOF
)
goto StartFunction

:StartFunction

Rem | Generate the list of items from textfile
FOR /F "delims=" %%A IN (%ItemList%) DO (
    set "ListAddition=!ListAddition!%%A, "
)
if "%ListAddition%"=="" (
    echo ERROR: File "%ItemList%" Is Empty!
    pause
    goto :EOF
) ELSE (set "ListAddition=!ListAddition:~0,-2!")

Rem | Get .ini To String
set HeaderFound=false
FOR /F "tokens=*" %%A IN ('type !CustomINI!') DO (

    Rem | First Find The Header "[Items]" & Extract "ListOfItems" Line Data
    for /f "tokens=*" %%B in ('echo %%A') do (
        set "item=%%B"
        if /i "!item!"=="!ListHeader!" (
            set HeaderFound=true
        ) else if "!HeaderFound!"=="true" (

            Rem | Turn Items For Line "ListOfItems" To String
            for /f "tokens=2,*" %%C in ('echo %%B') do (

               Rem | Set String
               set "SearchText=%%D"
            )
            Rem | Header Was Found, End Loop & goto HeaderContinue
            set HeaderFound=false
            goto HeaderContinue
        )

    )
)
Rem | Header Was Not Found
echo ERROR: The Header "%ListHeader%" Was Not Found!
pause
goto :EOF

:HeaderContinue

Rem | Check If "ListOfItems" Is Actually Populated
If "%SearchText%"=="" (
    Rem | Not Populated
    set "SearchText=!ListHeader!"
    set "AddTEXT=!Object! = !ListAddition!"
    goto EditMissingString
) ELSE (
    Rem | Populated
    set "REPLACETEXT=!SearchText!, !ListAddition!"
    goto EditExistingString
)

:EditExistingString

REM | Make sure we only edit the ListOfItems line.
FOR /F "delims=" %%n IN ('findstr /n "^" %CustomINI%') DO (
    SET line=%%n
    SET Modified=!line:%SearchText%=%ReplaceText%!
    SET Modified=!Modified:*:=!

    REM | Output the entire edited INI to a temporary file.
    >> %CustomINI%.TEMP ECHO(!Modified!
)

Rem | Delete Original File, Restore New
DEL %CustomINI%
REN %CustomINI%.TEMP %CustomINI%

goto :EOF

:EditMissingString

Rem | Add Text Under %ListHeader%
(
    FOR /f "delims=" %%i IN ('Echo !ListHeader!') DO (
    SET AddAfter=%%i

    FOR /f "delims=" %%n IN ('findstr /n "^" %CustomINI%') DO (
        SET line=%%n
        SET line=!line:*:=!

        ECHO(!line!
            IF "!line!"=="!AddAfter!" ECHO(%AddTEXT%
        )
    )
)>>%CustomINI%.TEMP

Rem | Delete Original File, Restore New
DEL %CustomINI%
REN %CustomINI%.TEMP %CustomINI%

goto :EOF

PS::我知道您的find命令位于其他位置,或者只是在脚本中将命令find更改为%WINDIR%\System32\FIND.exe

调试/更改:

抓取。


要获取有关任何命令的帮助,请执行以下操作:

  • call /?
  • set /?
  • for /?
  • if /?
  • find /?
  • 等等。