String 字符串批处理中的If语句不';你不能返回真值吗?
上面的代码是一个程序,它应该允许您键入一个快捷方式,将计算机名存储在变量nick中 我是新来的,仅供参考 当我运行该程序时,第一个if语句不管发生什么都会执行(即使%target%是不同的值)。我怎么会喜欢这个?我对比较字符串做了彻底的研究,但没有一个有效 另外,我需要在:shutdown页面中使用nick,因此我确实需要根据用户输入的内容设置该值。而且,(计算机)不是程序的一部分;它们是我为了隐私而替换的真实计算机名 谢谢String 字符串批处理中的If语句不';你不能返回真值吗?,string,batch-file,comparison,batch-processing,String,Batch File,Comparison,Batch Processing,上面的代码是一个程序,它应该允许您键入一个快捷方式,将计算机名存储在变量nick中 我是新来的,仅供参考 当我运行该程序时,第一个if语句不管发生什么都会执行(即使%target%是不同的值)。我怎么会喜欢这个?我对比较字符串做了彻底的研究,但没有一个有效 另外,我需要在:shutdown页面中使用nick,因此我确实需要根据用户输入的内容设置该值。而且,(计算机)不是程序的一部分;它们是我为了隐私而替换的真实计算机名 谢谢 如果“%target%”==“(computer1)”do(应为 如果
如果“%target%”==“(computer1)”do(
应为
如果“%target%”==(计算机1)(set nick=(计算机2)
(以及类似内容)应为
设置尼克=^(计算机2^)ELSE
的synax错误,应该是)ELSE(
: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
我简化了代码的IF块
正如JosefZ提到的,在代码中使用“do”是错误的(语法错误)
该错误导致忽略代码的“first if statement”,并使下一行“set nick=(computer1)”被执行。这就是为什么它看起来“first if statement会执行,不管发生什么”。通过“first if statement会执行,不管发生什么”,您的意思是变量“nick”变成了(computer1)'代码执行后?@Fumu7是的,无论用户输入的值是什么,变量nick都设置为(computer1)。糟糕!忘了更改if语句中的字符串。现在可能更容易理解@Fumu7谢谢,删除了“DO”关键字did it.:)
: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