@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)呼应线
- 如果找到(非errorlevel 1,so errorlevel为0)
- 检查“MC”
- 如果没有找到,输出与行“ MC”附加否则只输出线
然后删除临时文件,并显示一个报告的存在: 原来MYFILE.INI使用 输出NEWFILE.INI 和它们之间的差异。
将行检查写入临时文件意味着像批处理重定向器<
和>
这些尴尬的字符正确处理。正常的方法是ECHO %%x|findstr...
,但>
卡在管道中并泄漏出错信息。
关于唯一的问题是空行和可能行开始';'将从输出中删除,但由单独的空格组成的行将被保留...
+1,非常好 – Endoro 2013-03-23 07:27:10
梦幻般的工作。那里有几件物品,我必须自己研究,但这应该很好。非常感谢。 +1 – proggrock 2013-03-23 17:44:13