I've been trying to write a small programm that checks if one or more services are running and if so, terminates these services. Apart from trying it on my own, I also looked up some examples in google and finally came up with the following mixture of own code and found code:
Code: Select all
setlocal
@echo off
cls
rem ======================================================================
::
:: Service Searcher And Terminator
::
rem ----------------------------------------------------------------------
:: 2011-06-06 v1.0 NoNameFound
rem ======================================================================
rem ----------------------------------------------------------------------
:: CONFIG
rem ----------------------------------------------------------------------
set lServices=
set lServices=%lServices% "Spooler"
set tmpfile=%temp%\services_.tmp
rem ----------------------------------------------------------------------
:: MAIN
rem ----------------------------------------------------------------------
color 08
echo.
echo ===== SERVICE SEARCH AND DESTROY =====
echo.
call :getStatus
echo Stopped - %Stopped% .. Running - %Running%
if "%Stopped%" == "N" call :stopServices
echo.
color cf
echo.
echo --- INFO ---
echo Service is not running.
echo.
goto end
rem ----------------------------------------------------------------------
:: SUBS
rem ----------------------------------------------------------------------
:getStatus
set Stopped=N
set Running=N
echo.
echo Reading service state ...
net start >"%tmpfile%"
for %%a in (%lServices%) do call :checkService %%a
del "%tmpfile%"
echo.
goto end2
:checkService
echo.
findstr /c:%1 "%tmpfile%" >nul && ( echo YES: %1 is running && set Running=Y && goto end2)
echo NO: %1 is stopped && set Stopped=Y
goto end2
:stopServices
color 17
echo --- Stopping service ---
pause
for %%a in (%lServices%) do (
echo ... stopping %%a ...
echo.
net stop %%a
)
call :getStatus
goto end
rem ----------------------------------------------------------------------
:: END
rem ----------------------------------------------------------------------
:end
echo --- done ---
endlocal
pause
:end2
rem ----------------------------------------------------------------------
:: EOF
rem ----------------------------------------------------------------------
The first if condition under :Main "if "%Stopped%" == "N" call :stopServices" is completly ignored, although the variable is infact set to "N".
Can someone shed some light on why this is the case?
Cheers and thanks,
Fred