Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 按字符串中具有类似变量的标记从批处理中编辑txt文件_Batch File_Edit_Ini_String - Fatal编程技术网

Batch file 按字符串中具有类似变量的标记从批处理中编辑txt文件

Batch file 按字符串中具有类似变量的标记从批处理中编辑txt文件,batch-file,edit,ini,string,Batch File,Edit,Ini,String,我试图从批处理文件中编辑一个简单的txt ini文件,而不使用实用程序 多亏了paxdiablo之前发布的代码,我可以用下面的一种代码形式来实现这一点 但是,myfile.ini包含像thing和newthing这样的变量 我只想编辑第一个,而不是第二个(包括字符串“thing”) 我不想更改/编辑新事物的值 现在,每次编辑都会为这两个变量设置值 @echo off set init=50M set max=75M setlocal enableextensions enabledelayede

我试图从批处理文件中编辑一个简单的txt ini文件,而不使用实用程序

多亏了paxdiablo之前发布的代码,我可以用下面的一种代码形式来实现这一点

但是,myfile.ini包含像thingnewthing这样的变量

我只想编辑第一个,而不是第二个(包括字符串“thing”)

我不想更改/编辑新事物的值

现在,每次编辑都会为这两个变量设置值

@echo off
set init=50M
set max=75M
setlocal enableextensions enabledelayedexpansion
for /f "tokens=* delims=" %%a in (myfile.ini) do (
    set str1=%%a
    call :morph
    echo !str2!>>myfile_new.ini
    echo !str2!
)
endlocal
goto :eof

:morph
    set str2=
:morph1
    if not "x!str1!"=="x" (
        if "!str1:~0,6!"=="thing=" (
            set str2=!str2!thing="!init!"
            set str1=!str1:~12!
            goto :morph1
        )
        if "!str1:~0,6!"=="thong=" (
            set str2=!str2!thong="!max!"
            set str1=!str1:~12!
            goto :morph1
        )
        set str2=!str2!!str1:~0,1!
        set str1=!str1:~1!
        goto :morph1
    )
    goto :eof
myfile.ini

thing=xyz
thong=def
newthing=abc

您的代码似乎有点太复杂了。
我减少了它,使它工作,但也许我误解了你的问题。
但通过这种方式,它可以处理
myfile.ini
中的示例字符串

@echo off
set init=50M
set max=75M
setlocal enableextensions enabledelayedexpansion
for /f "tokens=* delims=" %%a in (myfile.ini) do (
    set str1=%%a
    call :morph
    echo !str1!>>myfile_new.ini
    echo !str1!
)
endlocal
goto :eof


:morph
if "!str1:~0,6!"=="thing=" (
    set "str1=thing=!init!"
)
if "!str1:~0,6!"=="thong=" (
    set "str1=thong=!max!"
)
goto :eof

您的代码似乎有点太复杂了。
我减少了它,使它工作,但也许我误解了你的问题。
但通过这种方式,它可以处理
myfile.ini
中的示例字符串

@echo off
set init=50M
set max=75M
setlocal enableextensions enabledelayedexpansion
for /f "tokens=* delims=" %%a in (myfile.ini) do (
    set str1=%%a
    call :morph
    echo !str1!>>myfile_new.ini
    echo !str1!
)
endlocal
goto :eof


:morph
if "!str1:~0,6!"=="thing=" (
    set "str1=thing=!init!"
)
if "!str1:~0,6!"=="thong=" (
    set "str1=thong=!max!"
)
goto :eof

为什么您不愿意使用外部工具
sed
awk
可以在一条线上完成任务(并且有用于windows的端口)。您需要一些匹配“行开始”的内容(这样您就可以避免为
内容
匹配另一个内容)。我能想到的唯一方法是在每一行前面加上一些东西(比如
@
),然后检查
@东西而不是只检查
东西,然后去掉
@
,但我不熟悉所需的语法。当然,您需要使用不能作为变量名一部分的前缀,否则您将在另一个伪装下遇到相同的问题。谢谢您的回复。为了让其他用户能够使用批处理,首选的方法是避免使用其他工具,但这可能是不可避免的,而且肯定更容易。将使用CHANGE.COM或EditMe.exe,但未找到64位版本(Win2k8)。txt ini的格式是固定的,不受我的控制。因为prepend仍然需要识别特定的行/字符串,但我知道newthing总是在thing之后。这可能有用。。。是否有某种方法可以使用的格式选择特定字符串!str1:~0,6!让我澄清一下预加的方法:在获得每一行(进入变量)后,将“@”(或任何标记)预加到变量本身(无需更改文件)。然后您可以检查
@thing=…
,您可以确定它将是正确的变量,
newthing
@newthing
将与
@thing
不匹配。替换该值后,在将该行写回文件之前,应先从变量中删除
@
。很抱歉,除了概念上的建议外,我对Windows批处理的研究还不够深入,无法提供任何建议:(为什么您不愿意使用外部工具?
sed
awk
可以在一条线上完成任务(并且有Windows的端口)。您需要与“线的起点”匹配的东西(因此,您可以避免为
对象
匹配另一个对象
)。我能想到的唯一方法是在每行前面加上一些内容(如
@
),然后检查
@thing
而不仅仅是
thing
,然后去掉
@
,但我不熟悉所需的语法。当然,您需要使用不能作为变量名一部分的前缀,否则您会在另一个伪装下遇到同样的问题。感谢您的回复。允许其他用户使用批处理,首选是避免使用其他工具,但这可能是不可避免的-当然更容易。将使用CHANGE.COM或EditMe.exe,但尚未找到64位版本(Win2k8)。并且txt ini的格式不受我的控制。因为prepend仍然需要识别特定的行/字符串,但我知道newthing总是在thing之后。这可能很有用…是否有某种方法可以使用!str1:~0,6!的格式来选择特定的字符串!让我澄清一下prepending aproach:获取每一行后(在变量中),在变量本身前面加上“@”(或任何标记)(无需更改文件)。然后你可以检查
@thing=…
,你可以确定它是正确的变量,
newthing
@newthing
将与
@thing
不匹配。替换值后,在将行写回文件之前,你会从变量中删除
@
。对不起,我从来没有深入过eno在Windows的批处理中提供概念建议以外的任何建议:(