2013-03-23 44 views
0

我完全新的命令脚本和可以使用一个小的方向追加字符串...Windows批处理文件,以获取通配符匹配线的值,检查字符串,如果需要

我需要检查,如果某在.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     
     ) 
      ) 

回答

2
@ECHO OFF 
SETLOCAL 
:temploop 
(SET tempfile="%temp%\temp%random%.#$#") 
IF EXIST %tempfile% GOTO temploop 

(FOR /f "delims=" %%x IN (myfile.ini) DO (
>%tempfile% ECHO %%x 
FINDSTR /b "Extmgr_Addins=" <%tempfile% >NUL 
IF ERRORLEVEL 1 (ECHO.%%x) ELSE (
    FIND "mc" <%tempfile% >NUL 
    IF ERRORLEVEL 1 (ECHO %%x,mc) ELSE (ECHO %%x) 
) 
) 
)>newfile.ini 
DEL %tempfile% 

ECHO ==== original file ===== 
type myfile.ini 
ECHO ==== modified file ===== 
type newfile.ini 
ECHO ==== differences ===== 
FC myfile.ini newfile.ini 
ECHO ==== end of report ===== 


GOTO :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 [email protected]#$%^&*()_+|\=-0<>,./?:;'[]{}" 
Extmgr_Addins=mc finally [email protected]#$%^&*()_+|\=-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 [email protected]#$%^&*()_+|\=-0<>,./?:;'[]{}",mc 
Extmgr_Addins=mc finally [email protected]#$%^&*()_+|\=-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 [email protected]#$%^&*()_+|\=-0<>,./?:;'[]{}" 
Extmgr_Addins=mc finally [email protected]#$%^&*()_+|\=-0<>,./?:;'[]{}" 
***** NEWFILE.INI 
mc and Extmgr_Addins= 
Extmgr_Addins=finally [email protected]#$%^&*()_+|\=-0<>,./?:;'[]{}",mc 
Extmgr_Addins=mc finally [email protected]#$%^&*()_+|\=-0<>,./?:;'[]{}" 
***** 

==== end of report ===== 

前几行处理建立一个有效的临时文件名。

然后我们

  1. 读取一行
  2. 倾倒到临时文件
  3. 检查与目标串
  4. 如果开头的行未找到(错误级别1)呼应线
  5. 如果找到(非errorlevel 1,so errorlevel为0)
  • 检查“MC”
  • 如果没有找到,输出与行“ MC”附加否则只输出线

然后删除临时文件,并显示一个报告的存在: 原来MYFILE.INI使用 输出NEWFILE.INI 和它们之间的差异。

将行检查写入临时文件意味着像批处理重定向器<>这些尴尬的字符正确处理。正常的方法是ECHO %%x|findstr...,但>卡在管道中并泄漏出错信息。

关于唯一的问题是空行和可能行开始';'将从输出中删除,但由单独的空格组成的行将被保留...

+0

+1,非常好 – Endoro 2013-03-23 07:27:10

+0

梦幻般的工作。那里有几件物品,我必须自己研究,但这应该很好。非常感谢。 +1 – proggrock 2013-03-23 17:44:13