Windows 在数组中查找变量-批处理

Windows 在数组中查找变量-批处理,windows,batch-file,dos,Windows,Batch File,Dos,我想通过批处理文件基于windows用户名创建快捷方式 我的思路是: if %username% in (a,b,c,d) ( shortcut ShortcutName DestinationPath ) else ( shortcut OtherShortcutName OtherDestinationPath ) 我对第一部分有问题,因为我已经知道如何通过命令行创建快捷方式 希望我能找到一些帮助。示例: @ECHO OFF &SETLOCAL if define

我想通过批处理文件基于windows用户名创建快捷方式

我的思路是:

if %username% in (a,b,c,d) (
    shortcut ShortcutName DestinationPath
)
else (
    shortcut OtherShortcutName OtherDestinationPath
)
我对第一部分有问题,因为我已经知道如何通过命令行创建快捷方式

希望我能找到一些帮助。

示例:

@ECHO OFF &SETLOCAL 
if defined Array[%username%] (
    shortcut ShortcutName DestinationPath
) else (
    shortcut OtherShortcutName OtherDestinationPath
)

还有一些代码可以让它更清晰:

@ECHO OFF &SETLOCAL 
set "Array[Peter]=true"
set "Array[James]=true"
set "Array[Robby]=true"
set "Array[Jimmy]=true"

set "MyUserName=Jimmy"
call:check "%MyUserName%"
set "MyUserName=Paul"
call:check "%MyUserName%"
goto:eof

:check
if defined Array[%~1] (
    echo %~1 is in the array.
) else (
    echo %~1 is NOT in the array.
)
exit /b
。。输出为:

Jimmy is in the array.
Paul is NOT in the array.
Not found
Not found
Robby is found
Not found
例如:

@ECHO OFF &SETLOCAL 
if defined Array[%username%] (
    shortcut ShortcutName DestinationPath
) else (
    shortcut OtherShortcutName OtherDestinationPath
)

还有一些代码可以让它更清晰:

@ECHO OFF &SETLOCAL 
set "Array[Peter]=true"
set "Array[James]=true"
set "Array[Robby]=true"
set "Array[Jimmy]=true"

set "MyUserName=Jimmy"
call:check "%MyUserName%"
set "MyUserName=Paul"
call:check "%MyUserName%"
goto:eof

:check
if defined Array[%~1] (
    echo %~1 is in the array.
) else (
    echo %~1 is NOT in the array.
)
exit /b
。。输出为:

Jimmy is in the array.
Paul is NOT in the array.
Not found
Not found
Robby is found
Not found

批处理文件中实际上没有数组类型,但我们可以通过使用
for
在一个空格分隔的列表上迭代来伪造它:

@ECHO OFF
set Array=Peter James Robby Jimmy

for %i in (%array%) do (if %i==%USERNAME% (echo %USERNAME% is found) else (echo %USERNAME% not found))
如果Robby已登录,则输出为:

Jimmy is in the array.
Paul is NOT in the array.
Not found
Not found
Robby is found
Not found

批处理文件中实际上没有数组类型,但我们可以通过使用
for
在一个空格分隔的列表上迭代来伪造它:

@ECHO OFF
set Array=Peter James Robby Jimmy

for %i in (%array%) do (if %i==%USERNAME% (echo %USERNAME% is found) else (echo %USERNAME% not found))
如果Robby已登录,则输出为:

Jimmy is in the array.
Paul is NOT in the array.
Not found
Not found
Robby is found
Not found

哇。。。选民们能告诉我哪里出了问题吗?请评论。哇。。。选民们能告诉我哪里出了问题吗?请评论。不适用于我(在Windows 7上)。我收到以下错误:
“arrayi”kann syntaktisch an dieser Stelle nicht verarbeitet werden。
更改为%%I in(%array%)do的
(如果%%I==%USERNAME%(找到了echo%USERNAME%)其他(找不到echo%USERNAME%)
使其工作对我不起作用(在Windows 7上)。我收到以下错误:
“arrayi”kann syntaktisch an dieser Stelle nicht verarbeitet werden。
更改为%%I in(%array%)do的
(如果%%I==%USERNAME%(找到了echo%USERNAME%)其他(找不到echo%USERNAME%)
使其正常工作