我该如何进行for循环,以便像所显示的逻辑那样使用for循环迭代字符串值:
set list[0]="string1"
set list[1]="string2"
...
set list[10]="string10"
for n in list do (
for /F "tokens=3 delims=: " %%H in ('sc query "list[n]" ^| findstr " STATE"') do (
if /I "%%H" NEQ "RUNNING" (
net stop list[n]
net start list[n] )
else( ) )
)
答案 0 :(得分:0)
将一些提示纳入代码中,
:: Q:\Test\2018\07\25\SO_51512357.cmd
@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
:: initialize/clear list
For /F "delims==" %%A in ('2^>Nul set list[') do Set "%%A="
:: use self expanding code to set list to array list[xx]
Set i=99
Set "list=,WPDBusEnum,WpnService,wscsvc,WSearch,wuauserv,WwanSvc"
Set "list=%list%,xbgm,XblAuthManager,XblGameSave,XboxGipSvc,XboxNetApiSvc"
Set "list=%list:,="&Set /a i+=1&Set "list[!i:~-2!]=%"
Set list
:: technically the following is one line, line breaks inserted where possible.
For /F "tokens=2delims==" %%n in (
'2^>Nul set list['
) do for /F "tokens=3 delims=: " %%H in (
'sc query "%%n" 2^>Nul ^| findstr /C:" STATE :"'
) do if /I "%%H" NEQ "RUNNING" (
echo net stop %%n
echo net start %%n
) else (
echo svc running %%n
)
示例输出:
> Q:\Test\2018\07\25\SO_51512357.cmd
list[00]=WPDBusEnum
list[01]=WpnService
list[02]=wscsvc
list[03]=WSearch
list[04]=wuauserv
list[05]=WwanSvc
list[06]=xbgm
list[07]=XblAuthManager
list[08]=XblGameSave
list[09]=XboxGipSvc
list[10]=XboxNetApiSvc
net stop WPDBusEnum
net start WPDBusEnum
svc running WpnService
svc running wscsvc
svc running WSearch
net stop wuauserv
net start wuauserv
net stop WwanSvc
net start WwanSvc
net stop xbgm
net start xbgm
net stop XblAuthManager
net start XblAuthManager
net stop XblGameSave
net start XblGameSave
net stop XboxGipSvc
net start XboxGipSvc
net stop XboxNetApiSvc
net start XboxNetApiSvc