Board logo

标题: [讨论] find.exe问题(多个变量时) [打印本页]

作者: chenhui530     时间: 2005-2-14 00:00    标题: [讨论] find.exe问题(多个变量时)

前些天就find.exe问题已经做出了解答假如在c:\123.txt里面有字符串“12345”
find "12345" c:\123.txt>nul
if not errorlevel 1 goto find_y
goto find_not
:find_y
echo find str
goto end
:find_not
echo find not str

:end
上面的代码是正确的我在几个系统里都测试了但是大家请看下面的代码就有问题,问题在于不知道问题出在什么地方不知道大家遇到过这个情况吗?代码如下:

set drv=c d e f g h i
for %%a in (%drv%) do find "12345" %%a:\123.txt>nul
if not errorlevel 1 goto find_y
goto find_not
:find_y
echo find str
goto end
:find_not
echo find not str

:end

这时问题就出来了不管字符串存在是否都没任何显示直接推出DOS如果把if语句改成if not errorlevel 3的话就都会显示找到文件。大家不妨试试究竟问题是出在哪个地方是不是find就不支持
多个分区文件找字符串只能对单个分区文件找字符串呢?
作者: Climbing     时间: 2005-2-17 00:00
问题的关键出在For循环上,for循环的特点决定了只是将最后一次检查的结果传给后面的if命令,所以你的批处理相当于只是检查了i:\123.txt中是否有指定的字符串。解决办法是:将for循环中后面的do命令换成另一个批处理,假设叫chk.bat,调用格式为for ... do call chk.bat %%a
@echo off
find "12345" %1 > nul
if not errorlevel 1 goto _gotit
goto _quit
:_gotit
set found=1
:_quit你的批处理相应改为:
set drv=c d e f g h i
set found=0
for %%a in (%drv%) do call chk.bat %%a
if %found%==1 goto find_y
goto find_not

:find_y
echo found str
goto end:find_not
echo not found str

:end
for %%I in (drv found) set %%I=

作者: chenhui530     时间: 2005-2-17 00:00
谢谢Climbing兄我已想出更简单的方法了代码如下:set drv=c d e f g h i
for %%a in (%drv%) do if exist %%a:\123.txt set a=%%a
find "12345" %a%:\123.txt>nul
if not errorlevel 1 goto find_y
goto find_not
:find_y
echo find str
goto end
:find_not
echo find not str

:end

作者: Climbing     时间: 2005-2-17 00:00
你的程序跟上一个程序在执行结果上没有什么太大的区别啊,除非你能保证只在一个盘下有123.txt。而我想象的是每个盘下都有这个文件,你没有交待清楚背景,当然就不同了。