I need to know the status of a service at the end of my batch script which restarts services using \"net stop thingie\" and \"net start thingie\".
In my most favorite id
Ros the code i post also is for knowing how many services are running...
Imagine you want to know how many services are like Oracle* then you put Oracle instead of NameOfSercive... and you get the number of services like Oracle* running on the variable %CountLines% and if you want to do something if there are only 4 you can do something like this:
IF 4==%CountLines% GOTO FourServicesAreRunning
That is much more powerfull... and your code does not let you to know if desired service is running ... if there is another srecive starting with same name... imagine: -ServiceOne -ServiceOnePersonal
If you search for ServiceOne, but it is only running ServiceOnePersonal your code will tell ServiceOne is running...
My code can be easly changed, since it reads all lines of the file and read line by line it can also do whatever you want to each service... see this:
@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
Of course it only list running services, i do not know any way net can list not running services...
Hope this helps!!!