我正在制作一个批处理脚本来停止多个服务,但是我在syntax is incorrect
serviceList.TEMP
错误
setlocal EnableDelayedExpansion
setlocal enableExtensions
::queryex output file
sc queryex>services.TEMP
find /i /N "DISPLAY_NAME: Hotspot" services.TEMP>tmp1.TEMP
FOR /F "skip=2" %%G in (tmp1.TEMP) do (
set num=%%G
set num=!num:~1,3!
echo !num!>serviceList.TEMP
)
FOR %%X in (serviceList.TEMP) do (
set /a "SKIP_LINES=%%G+7"
set secondForFilter="skip=%SKIP_LINES%"
FOR /F %secondForFilter% %%Z in (services.TEMP) do (
call debug.cmd REM debug.cmd -> echo debug pause>nul
set serv=%%Z
set "serv=!serv: =!" REM Extract PID
set "serv=!serv::=!" REM Extract PID
set procID=!serv!
taskkill /pid %procID% /f >>debug.txt 2>>debug.txt
goto secondLoopEnd
)
:secondLoopEnd
)
del /S *.TEMP >>debug.txt 2>>debug.txt
答案 0 :(得分:4)
问题在于:
FOR %%X in (serviceList.TEMP) do (
set /a "SKIP_LINES=%%G+7"
set secondForFilter="skip=%SKIP_LINES%"
FOR /F %secondForFilter% %%Z in (services.TEMP) do (
call debug.cmd REM debug.cmd -> echo debug pause>nul
在括号上下文中设置值时的常用方法是使用delayed expansion,但它不适用于参数化选项。
这里你需要一个子程序。
你在for循环中有GOTO
。 GOTO中断了上下文,并且在执行goto后不会调用循环。
并且rem不能与没有“&”
的代码在同一行上使用考虑这样的事情(虽然我无法检查蝙蝠的逻辑):
setlocal EnableDelayedExpansion
setlocal enableExtensions
::queryex output file
sc queryex>services.TEMP
find /i /N "DISPLAY_NAME: Hotspot" services.TEMP>tmp1.TEMP
FOR /F "skip=2" %%G in (tmp1.TEMP) do (
set num=%%G
set num=!num:~1,3!
echo !num!>serviceList.TEMP
)
FOR %%X in (serviceList.TEMP) do (
call :subroutine %%G
)
del /S *.TEMP >>debug.txt 2>>debug.txt
exit /b %errorlevel%
:subroutine
setlocal enableDelayedExpansion
set /a skiplines=%~1+7
set "filter="skip=%skiplines%""
FOR /F %filter% %%Z in (services.TEMP) do (
call debug.cmd
set serv=%%Z
rem extract PID
set "serv=!serv: =!"
set "serv=!serv::=!"
set procID=!serv!
taskkill /pid !procID! /f >>debug.txt 2>>debug.txt
goto :break_for
)
:break_for
endlocal
exit /b
答案 1 :(得分:1)
>在你的echoList.TEMP的回声是一个>>以便附加到文件?
echo !num!>>serviceList.TEMP
在这种情况下,您还应确保在追加操作之前删除该文件。
另外,我假设您错过了FOR循环中的/ F,因为您正在尝试读取serviceList.TEMP文件的行,是吗?
FOR %%X in (serviceList.TEMP) do (
应该......
FOR /F %%X in (serviceList.TEMP) do (
此外,您可以通过执行此操作将std out和err附加到同一文件...
someprocesshere 1> out.log 2>&1