2014-11-07 52 views
0
: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 

上面的代码是一个程序,应该允许您键入一个快捷方式来存储变量昵称中的计算机名称。如果批处理中的字符串中的语句不返回true?

我很新的批,FYI。

当我运行该程序时,无论如何执行第一个if语句(即使%target%是不同的值)。我该如何解决这个问题?我已经完成了比较字符串的深入研究,但都没有成功。

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

谢谢!

+0

通过句子“第一条if语句不管执行什么”,你的意思是说你的代码执行后变量'nick'变成'(computer1)'吗? – 2014-11-07 01:14:13

+0

@ Fumu7是的,无论用户输入什么值,变量昵称都被设置为(computer1)。 – ajc2000 2014-11-07 01:15:47

+0

我的坏!忘记更改if语句中的字符串。现在可能更容易理解@ Fumu7 – ajc2000 2014-11-07 01:19:48

回答

0
  1. IF "%target%"=="(computer1)" do(应该是

    IF “%的目标%” == “(电脑1)”(

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

    组缺口= ^(电脑2 ^)

  3. ELSE错误的synax,应该是

  4. 残缺的标签

+0

谢谢,删除了“DO”关键字。 :) – ajc2000 2014-11-07 02:25:43

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所说,在代码中使用“do”是错误的(语法错误)。

该错误会导致忽略代码中的'first if语句',并使下一行“set nick =(computer1)”被执行。这就是为什么它看起来“无论如何都是第一个执行语句”的原因。

相关问题