批处理文件,字符串变量比较,if语句 [英] Batch file, string variable comparison, if statement

查看:1113
本文介绍了批处理文件,字符串变量比较,if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件的日志与普通字符串进行比较,但如果进行比较则不会进入。我在echo语句中将Connect failed作为我的第二个令牌,但没有得到任何IF语句的结果。

I am trying to compare the logs of a file with a plain string, but it is not getting into if comparison. I am getting " Connect failed" as my 2nd token in echo statement, but not getting any result of IF statement.

@echo off
rem start cmd.exe
for /f "tokens=2 delims=:" %%n IN (C:\Users\rohit.bagjani\Desktop\result\telnetresult.txt) DO (
    SET str1 = " Connect failed"
    echo %%n
    if \i %str1%==%%n echo "true"
)
echo.
pause


推荐答案

第一个错误是排队:

SET str1 = " Connect failed"

这一行定义了一个名为 str1 的环境变量,其名称末尾有一个空格,值为连接失败的分配给它。前导空格和两个双引号也作为字符串的一部分分配给变量。

This line defines an environment variable with name str1  with a space at end of name with the value  " Connect failed" assigned to it. The leading space and the two double quotes are also assigned to the variable as part of the string.

作为为什么在命令行上使用'set var = text'后没有'echo%var%'的字符串输出?详细解释,正确的语法是:

As the answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? explains in detail, the right syntax would be:

set "str1=Connect failed"

此命令行定义环境变量 str1 ,并为其分配值连接失败

This command line defines an environment variable str1 with the value Connect failed assigned to it.

在命令提示符窗口 set /?中运行,以在多个显示页面上显示该命令的帮助。

Run in a command prompt window set /? to get displayed the help for this command on several display pages.

第二个错误在于:

if \i %str1%==%%n echo "true"

选项/开关在Windows上使用 / 指定, \ 用作目录分隔符。所以不区分大小写的比较开关必须是 / i 而不是 \ i

Options/switches are on Windows specified with / and \ is used as directory separator. So the switch for case-insensitive comparison must be /i and not \i.

在命令提示符窗口中运行 if /?获取有关 IF 命令的帮助。

Run in a command prompt window if /? for help on IF command.

第三个错误是尝试在命令块中定义环境变量,并为环境变量分配字符串值并引用此值环境变量不在同一命令块中使用延迟扩展。

The third mistake is the attempt to define an environment variable within a command block with assigning a string value to the environment variable and reference the value of this environment variable not using delayed expansion in same command block.

每当Windows命令解释器遇到一个开始的圆括号是解释为命令块的开头,它将所有内容解析为匹配的括号并替换用%VariableName%通过环境变量的当前值。

Whenever Windows command interpreter encounters an opening round bracket ( being interpreted as begin of a command block, it parses everything to matching parenthesis ) and replaces all environment variable references done with %VariableName% by current value of the environment variable.

在发布的代码中,这意味着行

In posted code this means the line

if \i %str1%==%%n echo "true"

改变了通过Windows命令解释程序

is changed by Windows command interpreter to

if \i == %n echo "true"

因为环境变量 str1 FOR 之前执行>未在 FOR 命令块上方定义。

before FOR is executed at all because of environment variable str1 is not defined above the FOR command block.

通过更改 echo off 回显或删除行 echo off 或用命令<$ c $注释掉c> rem 并在命令提示符窗口中运行批处理文件。然后Windows命令解释程序在执行前预处理后输出每个命令块和每个命令行。

This can be easily seen by changing echo off to echo on or remove the line with echo off or comment it out with command rem and run the batch file from within a command prompt window. Then Windows command interpreter outputs each command block and each command line after preprocessing before execution.

双击批处理文件执行它是不好的,因为窗口自动关闭在批处理退出时因为像这样的语法错误。 暂停的使用没有帮助,因为在 cmd.exe 。

Double clicking on a batch file to execute it is not good as the window is automatically closed on an exit of batch processing because of a syntax error like this one. The usage of pause is no help as this command line is not reached at all on detecting a syntax error by cmd.exe.

解决方案是:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /F "usebackq tokens=2 delims=:" %%I in ("%USERPROFILE%\Desktop\result\telnetresult.txt") do (
    set "str1=Connect failed"
    echo %%I
    if /I "!str1!" == "%%~I" echo true
)
endlocal
echo/
pause

但很多更简单,也可以工作:

But much easier and also working would be:

@echo off
for /F "usebackq tokens=2 delims=:" %%I in ("%USERPROFILE%\Desktop\result\telnetresult.txt") do (
    echo %%I
    if /I "Connect failed" == "%%~I" echo true
)
echo/
pause

使用 echo / 代替 echo。输出空林的原因请参阅在Windows CMD文件中,斜杠后面的回显是什么?

For the reason using echo/ instead of echo. to output an empty line see What does an echo followed immediately by a slash do in a Windows CMD file?

使用 I 或任何其他大写字母而不是 n 作为循环变量更多安全。为什么?在命令命令窗口中运行/?并阅读输出帮助,同时解释%~nI 。在批处理文件中使用 %% ~n 时,如果循环变量的当前值 n 应该与周围的双引号一起使用,或者在修饰符 ~n 之后缺少循环变量时会出现语法错误。循环变量区分大小写。大写字母的使用避免了使用修饰符解释循环变量时的冲突。

The usage of I or any other upper case letter instead of n as loop variable is more safe. Why? Run in a command command window for /? and read the output help explaining also %~nI. On usage of %%~n in a batch file it could be unclear for Windows command interpreter if the current value of loop variable n should be used with surrounding double quotes removed or there is a syntax error as the loop variable is missing after modifier ~n. Loop variables are case-sensitive. The usage of upper case letters avoids conflicts in interpretation of loop variables with modifiers.

为了理解使用的命令及其工作方式,打开命令提示符窗口,在那里执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.


  • echo /?

  • endlocal /?

  • for / ?

  • if /?

  • 暂停/?

  • 设置/?

  • setlocal /?

  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • pause /?
  • set /?
  • setlocal /?

请参阅维基百科有关 Windows环境变量,其中包含预定义的环境变量列表,其中包含 USERPROFILE 等描述。

See Wikipedia article about Windows Environment Variables for a list of predefined environment variables with description like USERPROFILE.

这篇关于批处理文件,字符串变量比较,if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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