如果批处理字符串中的语句不返回true?

时间:2014-11-07 00:53:32

标签: string batch-file comparison batch-processing

:start
net view
set nick=
echo.
echo.
echo If you want to select a common target, enter one of the following:
echo (computer1)
echo (computer2)
echo (computer3)
set /P target="Enter one of the above computer names (without \\):
IF "%target%"=="(computer1)" do(
set nick=(computer1)
goto shortcut
)
IF "%target%"=="(computer2)" do(
set nick=(computer2)
goto shortcut
)
 IF "%target%"=="(computer3)" do(
    set nick=(computer3) 
   goto shortcut

)
ELSE goto shutdown

上面的代码是一个程序,它允许您输入一个快捷方式,将计算机名称存储在变量nick中。

我很擅长批量,仅供参考。

当我运行程序时,无论如何都会执行第一个if语句(即使%target%是不同的值)。我该如何重新开始呢?我已经做过彻底的研究,比较字符串,没有一个有效。

P.S。我需要在:shutdown页面中使用nick,所以我真的需要根据用户输入的内容设置该值。此外,(计算机)不是该计划的一部分;它们是我为了隐私而替换的实际计算机名称。

谢谢!

2 个答案:

答案 0 :(得分:0)

  1. IF "%target%"=="(computer1)" do(应为

    如果“%target%”==“(computer1)”(

  2. set nick=(computer2)(和类似的)应该是

    设置nick = ^(computer2 ^)

  3. ELSE有错误的synax,应为) ELSE (

  4. 缺少标签

答案 1 :(得分:0)

尝试以下代码

:start
net view
set nick=
echo.
echo.
echo If you want to select a common target, enter one of the following:
echo (computer1) = home
echo (computer2) = guy
echo (computer3) = andre
set /P target="Enter one of the above computer names (without \\):"

IF "%target%"=="home" set nick=(computer1)
IF "%target%"=="andre" set nick=(computer2)
IF "%target%"=="guy"   set nick=(computer3)
IF "%nick%"=="" goto shutdown
goto shortcut

:shortcut
   Rem program for shotcut may be placed here
   echo "%target%"
   goto end

:shutdown
   Rem program for shutdown may be placed here

:end

我简化了代码的IF块。

正如JosefZ所提到的那样,使用"做"在你的代码中是错误的(语法错误)。

该错误导致忽略'首先if语句'你的代码,并制作下一行"设置nick =(computer1)"被执行。这就是它看起来"第一个if语句执行的原因,无论是什么"。

相关问题