批处理文件:如何从具有特殊字符的文本文件中读取和输出一行

时间:2013-12-10 15:50:45

标签: batch-file

我有一个批处理文件,它从文本文件中读取行,如果它们不包含字符串,则将它们写入另一个文本文件。 但是,我收到了一个错误:

我的代码:

:Read
setlocal EnableDelayedExpansion enableextensions
for /F "tokens=* delims=" %%i in ('findstr /n $ C:\Preview.txt') do set "str1=%%i"

echo !str1!

if not x%str1:"^/DATA^/"=%==x%str1% echo "!str1!" >> !_MERGEREPORTNAME! 2>&1

endlocal

我猜它在回声中工作得很好!str1!因为它确实回应了字符串,即

  

4:合并,编辑:   $ / DEVSRC / 2013 / REL / 13-REL-10 /数据库/ PRODUCT_CATALOG / DATA /国   cripts / RatePlan-WI-1 / MLQ-StateScripts / s_Coverage.sql; C981~C981 - >   $ / DEVSRC / 2013 /   PCH / 13-REL-10-P2 /数据库/ Product_Catalog / DATA / StateScripts / RatePlan-WI-1 / MLQ-S tateScripts / s_Coverage.sql; C313

然后它出错了“编辑:此时出乎意料。”

任何帮助提示都将受到赞赏。

编辑:这是我的文本文件,它会一直在改变

Conflict (merge, edit): $/DEVSRC/2013/REL/13-REL-10/Databases/Product_Catalog/StateScripts/RatePlan-WI-1/MLQAgent-StateScripts/s_Coverage.sql;C981~C981 -> $/DEVSRC/2013/PCH/13-REL-10-P2/Databases/Product_Catalog/StateScripts/RatePlan-WI-1/MLQAgent-StateScripts/s_Coverage.sql;C655
merge, edit: $/DEVSRC/2013/REL/13-REL-10/Databases/Product_Catalog/DATA/BaseScripts/b_App_Fabric_Cache_Reset.sql;C981~C981 -> $/DEVSRC/2013/PCH/13-REL-10-P2/Databases/Product_Catalog/DATA/BaseScripts/b_App_Fabric_Cache_Reset.sql;C313
merge, edit: $/DEVSRC/2013/REL/13-REL-10/Databases/Product_Catalog/DATA/BaseScripts/b_Document_Type.sql;C981~C981 -> $/DEVSRC/2013/PCH/13-REL-10-P2/Databases/Product_Catalog/DATA/BaseScripts/b_Document_Type.sql;C313
merge, edit: $/DEVSRC/2013/REL/13-REL-10/Databases/Product_Catalog/StateScripts/RatePlan-WI-1/MLQ-StateScripts/s_Coverage.sql;C981~C981 -> $/DEVSRC/2013/PCH/13-REL-10-P2/Databases/Product_Catalog/StateScripts/RatePlan-WI-1/MLQ-StateScripts/s_Coverage.sql;C313

还不能回答我自己的问题,但这有效:

这很有用。感谢foxidrive正确的方向看

setlocal DisableDelayedExpansion enableextensions
for /F "tokens=* delims=" %%i in ('findstr /n $ C:\Preview.txt') do (set "str1=%%i"
setlocal EnableDelayedExpansion

echo !str1!

set "search=/DATA/"

echo "!str1!"|find "!search!">nul && (
echo found
echo "/DATA/ files excluded" >> !_MERGEREPORTNAME! 2>&1
)||(
echo not FOUND
echo "!str1!" >> !_MERGEREPORTNAME! 2>&1
)

)
endlocal

2 个答案:

答案 0 :(得分:1)

如果没有/ DATA /

,这将回显该行
echo "!str1!"|find "/DATA/">nul || echo "!str1!" >> !_MERGEREPORTNAME! 2>&1

答案 1 :(得分:0)

我建议

if not "x%str1:/DATA/=%"=="x%str1%" echo "!str1!" >> !_MERGEREPORTNAME! 2>&1