Ros 我发布的代码也是为了了解有多少服务正在运行...
想象一下,您想知道有多少服务类似于 Oracle*,然后您将 Oracle 替换为 NameOfSercive...,您将获得在变量 %CountLines% 上运行的 Oracle* 等服务的数量,如果您想做某事,如果只有 4 个你可以做这样的事情:
IF 4==%CountLines% GOTO FourServicesAreRunning
这更强大...并且您的代码不会让您知道所需的服务是否正在运行...如果有另一个以相同名称开头的 srecive...想象一下:
-ServiceOne
-ServiceOnePersonal
如果您搜索 ServiceOne,但它只在运行 ServiceOnePersonal,您的代码会告诉 ServiceOne 正在运行...
我的代码可以轻松更改,因为它读取文件的所有行并逐行读取它还可以对每个服务执行任何您想要的操作...请参阅:
@ECHO OFF
REM Put here any code to be run before check for Services
SET TemporalFile=TemporalFile.TXT
NET START > %TemporalFile%
SET CountLines=0
FOR /F "delims=" %%X IN (%TemporalFile%) DO SET /A CountLines=1+CountLines
SETLOCAL EnableDelayedExpansion
SET CountLine=0
FOR /F "delims=" %%X IN (%TemporalFile%) DO @(
SET /A CountLine=1+CountLine
REM Do whatever you want to each line here, remember first and last are special not service names
IF 1==!CountLine! (
REM Do whatever you want with special first line, not a service.
) ELSE IF %CountLines%==!CountLine! (
REM Do whatever you want with special last line, not a service.
) ELSE (
REM Do whatever you want with rest lines, for each service.
REM For example echo its position number and name:
echo !CountLine! - %%X
REM Or filter by exact name (do not forget to not remove the three spaces at begining):
IF " NameOfService"=="%%X" (
REM Do whatever you want with Service filtered.
)
)
REM Do whatever more you want to all lines here, remember two first are special as last one
)
DEL -P %TemporalFile% 2>nul
SET TemporalFile=
REM Put here any code to be run after check for Services
当然它只列出正在运行的服务,我不知道 net 可以通过什么方式列出未运行的服务...
希望对你有帮助!!!