0

此代码可在批处理文件中自行查找

 for /F "delims=" %%i in ('findstr /e /v "return code: 0" login.txt') do (
     echo %%i found a string that matched for example return code 1 login.txt
     goto :eof
  )
  :eof

login.txt 的内容在哪里

   tstxtract return code: 0
   tstxtract return code: 0
   tstxtract return code:   1      

因此,如果有任何行与搜索条件不匹配,则执行 echo 命令。到目前为止一切顺利(感谢用户 zfus)

但是,我将相同的代码放在另一个脚本中,这次我将文件名作为另一个函数的变量传递.. find 命令发生了一些奇怪的事情。首先让我给你看剧本

 :extract
  dir /b /a-d | find /v "pstlist">pstlist
  FOR /F %%i in (pstlist) do (
  set pstname=%%i
  if exist c:\tokyo\scripts\output\%%i (
   mkdir C:\output\%%i_%todaydate%_%now%    
  echo **************** extraction result  >> c:\%extractlog%
  extract.exe -i -l c:\logs >> c:\%extractlog%   


 ) else (
    mkdir C:\output\%%i
     echo **************** extraction result  >> c:\%extractlog%
     extract.exe -i -l c:\logs >> c:\%extractlog%   
 ) 
 )
 goto chklog

 :chklog

     for /F "delims=" %%i in ('findstr /e /v "return code: 0" C:\%extractlog%') do (
          echo %%i found a string that matched for example return code 1 login.txt
           goto :eof
      )
     :eof

  :eof
   echo end & exit 

好的,请留在我身边.. 好的,如您所见,我现在正在传递 C:\%extractlog% 这是我希望 findstr 命令查看 chklog 函数的日志文件的位置。

此时 chklog 函数与我在上面显示的代码相同,唯一的区别是它被另一个函数调用并且一个变量被传递给它。

那么问题是什么?

findstr 命令现在不查看文件 c:\%extractlog% 的内容,而是查看 %extractlog% 文件名。因此,如果 %extraactlog% 名称是 12345.txt,那么 findstr 命令会在文件名中而不是在其内容中查找并查找 serach 条件?因此,它甚至不再查看文件内部。它正在查看文件名?我究竟做错了什么 ?谁能告诉我>? 为什么 findstr 命令没有查看 %extractlog% 文件的内容?

提前谢谢大家

4

1 回答 1

1

findstr命令使用 /C 开关指定要搜索的字符串。在我看来,C:\文件名的 被用作此选项。我建议您以这种方式将文件名括在引号中:

for /F "delims=" %%i in ('findstr /e /v "return code: 0" "C:\%extractlog%"') do (
于 2013-09-19T10:24:22.897 回答