Will this fail or better way of scripting?

2019-09-03 08:27发布

Will this fail or better way of scripting/coding the following? (particularly focusing on the first part of the script)
Should I be using ELSE statements etc.? Any recommendations would be greatly appreciated!

echo Setting Static IP Information... 
IF %POSID%==11 set IP_Addr=10.102.%DROPZERO%.105
IF %POSID%==12 set IP_Addr=10.102.%DROPZERO%.106
IF %POSID%==13 set IP_Addr=10.102.%DROPZERO%.107
IF %POSID%==14 set IP_Addr=10.102.%DROPZERO%.108
IF %POSID%==15 set IP_Addr=10.102.%DROPZERO%.109
IF %POSID%==21 set IP_Addr=10.102.%DROPZERO%.110
IF %POSID%==22 set IP_Addr=10.102.%DROPZERO%.111
IF %POSID%==23 set IP_Addr=10.102.%DROPZERO%.112
IF %POSID%==24 set IP_Addr=10.102.%DROPZERO%.113
IF %POSID%==25 set IP_Addr=10.102.%DROPZERO%.114

set D_Gate=10.102.%DROPZERO%.1
set Sub_Mask=255.255.255.0
netsh interface ip set address "!adapterName!" static %IP_Addr% %Sub_Mask% %D_Gate% 1 > output_net.txt
netsh interface ip set dns name="!adapterName!" static 10.98.1.26 primary >> output_net.txt
netsh interface ip add dns name="!adapterName!" 10.98.1.48 index=2 >> output_net.txt

1条回答
聊天终结者
2楼-- · 2019-09-03 09:07

This is the way I would do it:

setlocal EnableDelayedExpansion

echo Setting Static IP Information... 
set i=105
for %%a in (11 12 13 14 15  21 22 23 24 25) do (
   set posNum[%%a]=!i!
   set /A i+=1
)
set IP_Addr=10.102.%DROPZERO%.!posNum[%POSID%]!

For further details on array management in Batch files, see: this post

查看更多
登录 后发表回答