Dos - 如何根据用户输入检查变量名称?

时间:2016-01-27 15:32:04

标签: dos

我有以下批量代码,我想检查用户输入的时间,例如SB1作为输入,存在SB1 Varialbe Name。就我而言,它确实存在。但是如果用户输入SX1,那么它就不会退出。

@ECHO OFF
set "SB1=hannlsb1.mydomain.com:30515 -i 05"
set "DB1=hannlsd2.mydomain.com:31315    -i 13"
set "QB1=hannlsqa1-1:30115 -i 01"
set "DB0=hannlsd1.mydomain.com  -i 10"
set "QB0=hannlsps1-1.mydomain.com-i 03"
set "WB1=hannlsqa1-1:30315 -i 03"
set "VB1=hannlsvt-1.mydomain.com    -i 01"


@ECHO OFF
SET /P sysid=Please enter SystemID:
IF "%sysid%"=="" GOTO Error
REM ECHO Hello %sysid%, Welcome to DOS inputs!
GOTO End
:Error
ECHO You did not enter System ID! Bye bye!!
:End

1 个答案:

答案 0 :(得分:0)

这是使用FOR /F运行echo命令的一种方法,该命令将sysid扩展为表达式中间的值,然后再次扩展该值。

@echo off
set "SB1=hannlsb1.mydomain.com:30515 -i 05"
set "DB1=hannlsd2.mydomain.com:31315    -i 13"
set "QB1=hannlsqa1-1:30115 -i 01"
set "DB0=hannlsd1.mydomain.com  -i 10"
set "QB0=hannlsps1-1.mydomain.com-i 03"
set "WB1=hannlsqa1-1:30315 -i 03"
set "VB1=hannlsvt-1.mydomain.com    -i 01"

SETLOCAL ENABLEDELAYEDEXPANSION
SET /P sysid=Please enter SystemID:
FOR /F "usebackq delims==" %%i IN (`echo+!%sysid%!`) DO SET val=%%i
SETLOCAL DISABLEDELAYEDEXPANSION
IF NOT "%val%"=="" (
   ECHO Hello %sysid%, Welcome to DOS inputs!
) ELSE (
   ECHO You did not enter System ID! Bye bye!!
)

请注意,延迟扩展有一个令人遗憾的副作用,即启用时不能在echo命令中包含感叹号。