我正在处理与用户状态迁移工具一起使用的批处理文件。我试图允许多个用户被复制并以逗号分隔,然后将这些用户解析为scanstate
命令。
主要问题是我需要在使用2个其他变量的循环中引用一个变量。
我需要调用的变量名为“ user1”,当我调用变量的数字部分时,在遍历多个用户时需要使用%count%
。如果我放%user%%count%
无效,我也尝试过%user!count!%
目标是将存储在变量%user1%
至%user20%
中的用户名作为scanstate命令的参数输入(代替%users_to_copy%
:
scanstate.exe %DEFAULT_PATH%\data\%computername% /config:Config_SettingsOnly.xml /i:migapp.xml /i:miguser.xml /o /ue:*\* %users_to_copy%/localonly /listfiles:%DEFAULT_PATH%\data\%computername%\filelist.log /c"
set /p s=<users.txt
set counter=0
for %%a in (%s:,= %) do (
set /a counter+=1
set user!counter!=%%a
)
set users_to_copy =
set /a count = 1
:LOOP
if %count% LEQ %counter% (
set "users_to_copy=%users_to_copy%/ui:NET\%user%%count% /ui:%user%%count% "
set /a count +=1
goto :LOOP
)
使用所示示例运行时,"%user%%count%"
仅被解析为count的值,而不是%user1%
的值。
编辑:
由于评论中的建议而修复,我不得不使用!user%count%!。
set users_to_copy =
set /a count = 0
:LOOP
if %count% LEQ %counter% (
set /a count +=1
set "users_to_copy=%users_to_copy%/ui:%USERDOMAIN%\!user%count%! "
goto :LOOP
)
现在它以正确的值之前的空白值“ / ui:”循环了1次,但被scanstate.exe忽略了,这可能是我的循环而不是其他任何问题。