批量如何FINDSTR结果设置为变量,并禁用FINDSTR打印到控制台 [英] Batch how to set FINDSTR result to a variable and disable findstr print to console

查看:212
本文介绍了批量如何FINDSTR结果设置为变量,并禁用FINDSTR打印到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的批处理程序

FINDSTR  /C:"Result Comparison failure"  %tmp_result_file% 

我想要做的folloiwng,将上面的命令给一个变量的结果。
如果找到,设置第一行varible或将找到的所有线到varible对我来说是OK。

I want to do the folloiwng , set the result of the above command to a variable. If found, set the first line to varible or set the all found line to a varible is OK for me.

此外,上面条命令将打印findstr命令来安慰甚至@echo关闭。有没有禁用Print方法。

also the above commmand will print the findstr command to console even @echo off. is there any method to disable the print.

非常感谢

我的脚本的一部分,我做的是对的资源文件的每一行命令运行并把运行结果到一个tmp文件,然后使用STR找到找到失败的字符串来检查执行结果。

part of my script, what i do is run command on every line in sourcefile and put the run result into a tmp file, then use find str to find the failed string to check the run result.

for /f  %%a in (%source_file%) do (
    echo  run %%a >> %output_file%
    call  %run_script_command% %%a > %tmp_result_file% 2>&1
    ::notepad %tmp_result_file%
      for /f %%i in ('FINDSTR /C:"Result Comparison failure"  %tmp_result_file%') do  echo %%ixxx
    echo xx
)

很奇怪,结果是:

very strange , the result is:


    xx
    Resultxxx
    xx

背景是,我在%SOURCE_FILE%两个项目,所以出去跑2次。结果
对于第一个,FINDSTR无法找到任何东西,所以打印 XXX 结果
对于第二个,它找到一个在FINDSTR,但只打印出结果,而不是结果比较失败 ,还xx是之前打印的结果。很奇怪啊!

the background is that i have two items in %source_file%, so the out for run 2 times.
for the first one, the FINDSTR can not find anything, so print xxx
for the second one, it find one in findstr , but only print "Result" instead of "Result Comparison failure", also the xx is print before it in result. Very Strange!

推荐答案

第一个问题是,因为你只取了第一个令牌。为了解决这个问题,你有两个解决方案,要么呼应,其中串中发现的完整产品线...

The first problem is because you just take the first token from FOR. To solve it, you have two solutions, either to echo the complete line where the string is found...

for /f %%i "tokens=*" in ('FINDSTR /C:"Result Comparison Failure" %tmp_result_file%') do echo %%i

或回声找到了三个令牌

for /f %%i in ('FINDSTR /C:"Result Comparison Failure" %tmp_result_file%') do echo %%i %%j %%k

第二个问题,XX回荡两次是因为你运行该命令两次。第xx是一名来自第一次运行,第二个是从第二轮。如果您想prevent第二个,你需要使用一些额外的逻辑。例如,设置一个变量,然后检查它。警告,在一个循环中设置一个变量需要启用延迟扩展和使用!XX!语法(见详细解释HELP SET)

the second problem, the xx echoed two times is because you run two times the command. The first xx is the one from the first run, the second one is from the second run. If you want to prevent the second one, you need to use some additional logic. For example, setting a variable and then checking it. Warning, setting a variable in a loop requires enabling delayed expansion and using the !xx! syntax (see HELP SET for a detailed explanation)

setlocal enabledelayedexpansion
...
set result=
for /f %%i "tokens=*" in ('FINDSTR /C:"Result Comparison Failure" %tmp_result_file%') do (
  set result=%%i
)
if "!result!"=="" ( 
  echo !result!
) else (
  echo xx
)

这篇关于批量如何FINDSTR结果设置为变量,并禁用FINDSTR打印到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆