Scripting Windows批处理文件要获取通配符匹配行的值,请检查子字符串并在需要时追加字符串

Scripting Windows批处理文件要获取通配符匹配行的值,请检查子字符串并在需要时追加字符串,scripting,batch-file,Scripting,Batch File,我是一个全新的命令脚本,可以使用一点方向 我需要检查.ini文件中是否存在某一行,但是我只知道该行的哪一部分。如果它存在,我知道它将以字符串“Extmgr_Addins=”开头,并且它可以具有任意数量的逗号分隔字符串值 如果我找到这一行,我需要看看是否包含子字符串“mc”。如果它已经有“mc”,那么什么也不做。如果没有,我需要将字符串“、mc”附加到该行的末尾。这就是我到目前为止所做的: for /f "TOKENS=*" %%x in (myFile.ini) do ( set line=%

我是一个全新的命令脚本,可以使用一点方向

我需要检查.ini文件中是否存在某一行,但是我只知道该行的哪一部分。如果它存在,我知道它将以字符串“Extmgr_Addins=”开头,并且它可以具有任意数量的逗号分隔字符串值

如果我找到这一行,我需要看看是否包含子字符串“mc”。如果它已经有“mc”,那么什么也不做。如果没有,我需要将字符串“、mc”附加到该行的末尾。这就是我到目前为止所做的:

for /f "TOKENS=*" %%x in (myFile.ini) do (

set line=%%x
set line=!tst:Extmgr_Addins!

if not !line!==%%x ( 
     REM matched string ExtMgr_Addins
   set hasMC = %%x
   set hasMC=!hasMC:mc!

   if not !line!==%%x (

        REM matched substring of mc - do nothing

        )else(

                         REM here is where I would append "mc" to this line                 
        )
            )
@ECHO关闭
SETLOCAL
:temploop
(设置tempfile=“%temp%\temp%random%.#$#”)
如果存在%tempfile%GOTO temploop
(对于(myfile.ini)中的/f“delims=“%%x)DO(
>%临时文件%ECHO%%x
FINDSTR/b“Extmgr\u Addins=“NUL
如果错误级别1(回显%%x)其他(
查找“mc”NUL
如果错误级别1(ECHO%%x,mc)其他(ECHO%%x)
)
)
)>newfile.ini
删除%tempfile%
ECHO===原始文件=====
键入myfile.ini
ECHO===修改过的文件=====
键入newfile.ini
回声===差异=====
FC myfile.ini newfile.ini
ECHO==报告结束=====
后藤:eof
结果:

tempfile="c:\temp\temp8291.#$#"
==== original file =====
notthisline
Extmgr_Addins=notargetstring
Extmgr_Addins=mchastargetstring
Extmgr_Addins=hastargetatendmc
 Extmgr_Addins=notbeginsnotargetstring
 Extmgr_Addins=notbeginsmchastargetstring
 Extmgr_Addins=notbeginshastargetatendmc
mcinthisline
thislinehasmctoo
andat the end mc
mc and Extmgr_Addins=
Extmgr_Addins=finally !@#$%^&*()_+|\=-0<>,./?:;'[]{}"
Extmgr_Addins=mc finally !@#$%^&*()_+|\=-0<>,./?:;'[]{}"
==== modified file =====
notthisline
Extmgr_Addins=notargetstring,mc
Extmgr_Addins=mchastargetstring
Extmgr_Addins=hastargetatendmc
 Extmgr_Addins=notbeginsnotargetstring
 Extmgr_Addins=notbeginsmchastargetstring
 Extmgr_Addins=notbeginshastargetatendmc
mcinthisline
thislinehasmctoo
andat the end mc
mc and Extmgr_Addins=
Extmgr_Addins=finally !@#$%^&*()_+|\=-0<>,./?:;'[]{}",mc
Extmgr_Addins=mc finally !@#$%^&*()_+|\=-0<>,./?:;'[]{}"
==== differences =====
Comparing files myfile.ini and NEWFILE.INI
***** myfile.ini
notthisline
Extmgr_Addins=notargetstring
Extmgr_Addins=mchastargetstring
***** NEWFILE.INI
notthisline
Extmgr_Addins=notargetstring,mc
Extmgr_Addins=mchastargetstring
*****

***** myfile.ini
mc and Extmgr_Addins=
Extmgr_Addins=finally !@#$%^&*()_+|\=-0<>,./?:;'[]{}"
Extmgr_Addins=mc finally !@#$%^&*()_+|\=-0<>,./?:;'[]{}"
***** NEWFILE.INI
mc and Extmgr_Addins=
Extmgr_Addins=finally !@#$%^&*()_+|\=-0<>,./?:;'[]{}",mc
Extmgr_Addins=mc finally !@#$%^&*()_+|\=-0<>,./?:;'[]{}"
*****

==== end of report =====
tempfile=“c:\temp\temp8291.-$”
==原始文件=====
不是这条线
Extmgr_Addins=notargetstring
Extmgr_Addins=mchastargetstring
Extmgr_Addins=hasTargetAttendMC
Extmgr_Addins=notbeginsnotargetstring
Extmgr_Addins=notbeginsmchastargetstring
Extmgr_Addins=notbeginshastargetatendmc
mcinthisline
这条线也有
最后呢
mc和Extmgr_插件=
Extmgr_Addins=最终!@$%^&*()_+|\=-0,./?:;'[]{}"
Extmgr\u Addins=mc finally!@$%^&*()。/?:;“[]{}”
==修改过的文件=====
不是这条线
Extmgr_Addins=notargetstring,mc
Extmgr_Addins=mchastargetstring
Extmgr_Addins=hasTargetAttendMC
Extmgr_Addins=notbeginsnotargetstring
Extmgr_Addins=notbeginsmchastargetstring
Extmgr_Addins=notbeginshastargetatendmc
mcinthisline
这条线也有
最后呢
mc和Extmgr_插件=
Extmgr_Addins=最终!@$%^&*()_+|\=-0,./?:;'[]{}”,mc
Extmgr\u Addins=mc finally!@$%^&*()。/?:;“[]{}”
==差异=====
比较文件myfile.ini和NEWFILE.ini
*****myfile.ini
不是这条线
Extmgr_Addins=notargetstring
Extmgr_Addins=mchastargetstring
*****NEWFILE.INI
不是这条线
Extmgr_Addins=notargetstring,mc
Extmgr_Addins=mchastargetstring
*****
*****myfile.ini
mc和Extmgr_插件=
Extmgr_Addins=最终!@$%^&*()_+|\=-0,./?:;'[]{}"
Extmgr\u Addins=mc finally!@$%^&*()。/?:;“[]{}”
*****NEWFILE.INI
mc和Extmgr_插件=
Extmgr_Addins=最终!@$%^&*()_+|\=-0,./?:;'[]{}”,mc
Extmgr\u Addins=mc finally!@$%^&*()。/?:;“[]{}”
*****
==报告结束=====
前几行涉及建立有效的临时文件名

那么我们

  • 读一行
  • 将其转储到临时文件中
  • 检查以目标字符串开头的行
  • 如果未找到(错误级别1),则回显该行
  • 如果找到(不是errorlevel 1,因此errorlevel为0)
  • 检查是否存在“mc”
  • 如果找不到,则输出附加有“.mc”的行,否则仅输出该行
  • 然后删除临时文件并显示报告: 使用的原始MYFILE.INI 输出为NEWFILE.INI 以及它们之间的区别

    将正在检查的行写入tempfile意味着可以正确处理像
    这样的笨拙字符,它们是成批的重定向器。通常的方法是
    ECHO%%x | findstr…
    ,但
    卡在管道中并泄漏错误消息


    唯一的问题是空行和可能以“;”开头的行将从输出中删除,但将保留由空格组成的行…

    精彩的作品。有几个项目在那里,我将不得不研究我自己,但这应该工作得很好。非常感谢+1.
    tempfile="c:\temp\temp8291.#$#"
    ==== original file =====
    notthisline
    Extmgr_Addins=notargetstring
    Extmgr_Addins=mchastargetstring
    Extmgr_Addins=hastargetatendmc
     Extmgr_Addins=notbeginsnotargetstring
     Extmgr_Addins=notbeginsmchastargetstring
     Extmgr_Addins=notbeginshastargetatendmc
    mcinthisline
    thislinehasmctoo
    andat the end mc
    mc and Extmgr_Addins=
    Extmgr_Addins=finally !@#$%^&*()_+|\=-0<>,./?:;'[]{}"
    Extmgr_Addins=mc finally !@#$%^&*()_+|\=-0<>,./?:;'[]{}"
    ==== modified file =====
    notthisline
    Extmgr_Addins=notargetstring,mc
    Extmgr_Addins=mchastargetstring
    Extmgr_Addins=hastargetatendmc
     Extmgr_Addins=notbeginsnotargetstring
     Extmgr_Addins=notbeginsmchastargetstring
     Extmgr_Addins=notbeginshastargetatendmc
    mcinthisline
    thislinehasmctoo
    andat the end mc
    mc and Extmgr_Addins=
    Extmgr_Addins=finally !@#$%^&*()_+|\=-0<>,./?:;'[]{}",mc
    Extmgr_Addins=mc finally !@#$%^&*()_+|\=-0<>,./?:;'[]{}"
    ==== differences =====
    Comparing files myfile.ini and NEWFILE.INI
    ***** myfile.ini
    notthisline
    Extmgr_Addins=notargetstring
    Extmgr_Addins=mchastargetstring
    ***** NEWFILE.INI
    notthisline
    Extmgr_Addins=notargetstring,mc
    Extmgr_Addins=mchastargetstring
    *****
    
    ***** myfile.ini
    mc and Extmgr_Addins=
    Extmgr_Addins=finally !@#$%^&*()_+|\=-0<>,./?:;'[]{}"
    Extmgr_Addins=mc finally !@#$%^&*()_+|\=-0<>,./?:;'[]{}"
    ***** NEWFILE.INI
    mc and Extmgr_Addins=
    Extmgr_Addins=finally !@#$%^&*()_+|\=-0<>,./?:;'[]{}",mc
    Extmgr_Addins=mc finally !@#$%^&*()_+|\=-0<>,./?:;'[]{}"
    *****
    
    ==== end of report =====