标题: 求教: 如何实现在文件中搜索一个字符串
[打印本页]
作者: linkejin
时间: 2011-1-21 16:06
标题: 求教: 如何实现在文件中搜索一个字符串
求教,我想在批处理脚本中实现在一系列文件中搜索一字符串,如果在每个文件中都搜索到,就打印成功,若有一个找不到,打印失败。
刚开始我想用find来实现的,但发现无论find是否能在文件中找到字符串,返回值均是零。
后来我又写了个脚本
@echo off
cd warn
for %%i in (*) do (
echo %%i
set testflag=false
for /F "delims=" %%j in (%%i) do (
if "%%j" == "PC-LINT Check Completed!" set testflag=yes
)
echo ==%testflag%===
if not "%testflag%" == "yes" goto :break
echo %%i is matched
)
set completed=true
echo %completed%
cd ..
goto :end
:break
echo "havenot completed"
cd ..
:end
遍历目录下的每个文件,看下面是否有对应的语句,结果发现,执行的效果很诡异
同样的程序,一次是成功的,一次是失败的
E:\dostest>test
cpeserver_warn
==false===
"havenot completed"
E:\dostest>test
cpeserver_warn
==false===
"havenot completed"
E:\dostest>test
cpeserver_warn
==yes===
cpeserver_warn is matched
db_warn
==yes===
db_warn is matched
dhcp6s_warn
==yes===
dhcp6s_warn is matched
true
作者: Hanyeguxing
时间: 2011-1-21 16:23
@echo off
for %%a in (*.txt) do findstr /ilc:"a" "%%a">nul 2>nul&&(echo;%%a有字符a)||(echo;%%a无字符a)
pause
[
Last edited by Hanyeguxing on 2011-1-21 at 16:30 ]