Batch file 条件批登录文件
是否可以在windows批处理文件中使用If…Else语句 我正在考虑以类似的方式更改windows登录脚本:Batch file 条件批登录文件,batch-file,login-script,Batch File,Login Script,是否可以在windows批处理文件中使用If…Else语句 我正在考虑以类似的方式更改windows登录脚本: If username=user1, user2, user3 ( *instructions here* ) Else If username=user4, user5, user6 ( *instructions here* ) Else ( *instructions here* ) 正确的方法是什么 谢谢。这是一个很
If username=user1, user2, user3 (
*instructions here*
)
Else If username=user4, user5, user6 (
*instructions here*
)
Else (
*instructions here*
)
正确的方法是什么
谢谢。这是一个很好的方法:
@echo off
set $L1=user1,user2,user3
set $L2=user4,user5,user6
for %%a in (%$L1%) do if /i "%username%"=="%%a" goto Case1
for %%a in (%$L2%) do if /i "%username%"=="%%a" goto Case2
goto Nocase
:Case1
echo User 1-3
exit /b
:Case2
echo User 4-6
exit /b
:Nocase
echo NO CASE
您可以使用其他列表($L3,$L4,…)轻松展开它。这是一种很好且强大的方法:
@echo off
set $L1=user1,user2,user3
set $L2=user4,user5,user6
for %%a in (%$L1%) do if /i "%username%"=="%%a" goto Case1
for %%a in (%$L2%) do if /i "%username%"=="%%a" goto Case2
goto Nocase
:Case1
echo User 1-3
exit /b
:Case2
echo User 4-6
exit /b
:Nocase
echo NO CASE
您可以使用其他列表(L3,$L4,…)轻松展开它。sachadee有一个很好的答案,即使用GOTO。但是,不需要后顾之忧,也可以很容易地达到目的 试图用列表变量中的任何内容替换当前用户名。如果结果与原始结果不同,则当前用户必须在列表中。请注意列表中的前导和尾随逗号。它们很重要
@echo off
setlocal enableDelayedExpansion
set "list1=,user1,user2,user3,"
set "list2=,user4,user5,user6,"
if "!list1:,%username%,=!" neq "!list1!" (
echo code for user 1-3 here
) else if "!list2:,%username%,=!" neq "!list1!" (
echo code for users 4-6 here
) else (
echo code for all others here
)
sachadee有一个使用GOTO的好答案。但是,不需要后顾之忧,也可以很容易地达到目的 试图用列表变量中的任何内容替换当前用户名。如果结果与原始结果不同,则当前用户必须在列表中。请注意列表中的前导和尾随逗号。它们很重要
@echo off
setlocal enableDelayedExpansion
set "list1=,user1,user2,user3,"
set "list2=,user4,user5,user6,"
if "!list1:,%username%,=!" neq "!list1!" (
echo code for user 1-3 here
) else if "!list2:,%username%,=!" neq "!list1!" (
echo code for users 4-6 here
) else (
echo code for all others here
)
如果有其他方法,您可以使用
和
和或
有点棘手。查看和[here](ss64.com/nt/if.html),您可以使用if-else和
和或
有点棘手。看看and[here](ss64.com/nt/if.html)@Mofi-Changing格式的goto
语句是可以的,但是goto:Case1
的原始语法工作得非常好,尽管看起来有点奇怪。其他编辑都很好。@dbenham-我用goto:Case1
执行了原始批处理文件,因此知道这也有效。但从语法上讲,它不是100%正确的。它之所以有效,是因为cmd.exe将冒号解释为分隔符字符,或者忽略冒号,因为冒号是标签的无效字符,除了goto:EOF
,请参见说明(如果,请参见相关页面上的说明)。堆栈溢出上的代码示例应该尽可能地“漂亮”,并且不会让代码变得古怪,尤其是在这里这样好的代码上。@Mofi-更改goto
语句的格式是可以的,但是goto:Case1
的原始语法工作得非常好,尽管看起来有点奇怪。其他编辑都很好。@dbenham-我用goto:Case1
执行了原始批处理文件,因此知道这也有效。但从语法上讲,它不是100%正确的。它之所以有效,是因为cmd.exe将冒号解释为分隔符字符,或者忽略冒号,因为冒号是标签的无效字符,除了goto:EOF
,请参见说明(如果,请参见相关页面上的说明)。关于堆栈溢出的代码示例应该尽可能的“漂亮”,而不是奇怪的代码,尤其是这里的好代码。