Solved Repeating taskill n times 5 times for /L loop problem

Netgear / Dg834g
January 26, 2012 at 12:31:14
Specs: Windows XP SP3, 4000+ / 2gb DDR
Please could someone help me here ?

I am not sure what I am doing wrong here the loop just keeps restarting over and over again.
Basically rather than duplicating a lot of code 5 times I am trying to run the following code 5 times and after each time it has run change a variable to the next variable before exiting the loop.

::BEGIN
:: @@@@@@@@@@@@@@@ S TURN OFF APPS @@@@@@@@@@@@@@@
goto setvari
:turnoff
set apps=0

for /f "tokens=*" %%1 in ('tasklist /nh ^|find "%clients%"') do (
for /f "tokens=5" %%A in ("%%1") do (
set apps=%%A
)
)

> task.txt echo %clients%
if not "%apps%"=="0" echo %apps% %%A &&>> client.txt echo ACTIVE %clients%
if not "%apps%"=="0" if not exist %powerswitch% echo call endapp
if "%apps%"=="0" echo %clients% is not running.
pause
del task.txt
if exist client.txt del client.txt
sleep 6
cls
:endloop
cls

:: @@@@@@@@@@@@@@@@ E TURN OFF APPS @@@@@@@@@@@@@@@@
:setvari
for /L %%i in (1 1 4) do (
if "%%i"=="1" set clients=CCleaner.exe && goto turnoff
if "%%i"=="2" set clients=test1.exe && goto turnoff
if "%%i"=="3" set clients=test2.exe && goto turnoff
if "%%i"=="4" set clients=winamp.exe && goto endloop
)

:endloop


See More: Repeating taskill n times 5 times for /L loop problem

Report •

#1
January 26, 2012 at 13:25:21
✔ Best Answer
I think it's because the Setvari loop is run just once (by the goto Setvari command line at the start of the script) and exits the loop after the first line so Clients is always set to CCleaner.exe.

My preferred method of achieving what you want is to use this kind of coding:

set Clients=CCleaner.exe
Call :rest 
set Clients=test1.exe
Call :rest
set Clients=test2.exe
Call :rest
set Clients= (etc etc)
Call :rest
exit /b     (or whatever else you want to do)

:rest
(the rest of your codings)

If you prefer to use embedded loops then your coding may have to run inside the For /L loop so that clients is reset to the new string (program name) on each loop.

Hope this helps.

Wahine.


Please come back & tell us if your problem is resolved.


Report •

#2
January 26, 2012 at 14:35:48
Dear Wahine

Thanks so much I have been struggling all day with this, ( I WILL TEST IT ) it's basically an add on to my other batch files to automate shutting down none essential programs during times of system instability, I am using a small text file that gets bigger over time with echo statements of system is stable once the file is above 4kb my batch switches FLAGS back on for none essential programs that run at startup, but if drwin.exe runs for some reason and is active for 3 subsequent passes of one of my system stability checks then this batch file (ABOVE) is put into action to kill any none essential applications until a stable history is re-established just thought a little detail so you know what it's for thanks :D

Michelle xxx


Report •

#3
January 26, 2012 at 15:19:33
Thanks Wahine got it working thanks so very much :)

Here is my partially completed Batch file the bit you helped me with is in the middle.

Michelle xxxx

@echo off && SetLocal EnableDelayedExpansion
:: ****************************************************************
:: @@@@@@@@@@@@@@@@@@@@@@@@@@ S CONSISTENCY @@@@@@@@@@@@@@@@@@@@@@@@@
:: ------------------------------------------------------------------
:: If subsequent loads are stable then NO VOICE NOTICES Will be given
:: All none essential programs will be sheduled to run normally.
:: If system is UN-STABLE for several scans via Que.bat then this batch
:: is called from their.
:: ------------------------------------------------------------------
:: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
set consistent="C:\state\stable.txt"
set state="C:\state\status.txt"
set powerswitch="C:\state\powerswitch.flg"
:: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
>> %consistent% echo stable on %date% %time%
for %%a in (%consistent%) do ( echo value= %%~za )
for %%a in (%consistent%) do ( if %%~za gtr 2100 (set conresult=1) )
:: -------------------------------------------
if "%conresult%"=="1" ECHO TRUE && goto stable
:unstable
if exist %powerswitch% del %powerswitch% && >> %state% echo Turning OFF The Power flags @ %date% %time%
if exist SayStatic.exe SayStatic SERVER ANNOUNCMENT: THE SERVER IS UNSTABLE.
echo %consistent% Is missing or below threshold size limit.
echo This means ...
echo.
echo The SERVER has had too many errors recently so none essential
echo programs are not being re-enabled at this time. Only important
echo services will be scheduled to run automatically until the problems
echo are resolved.
sleep 12
cls
goto endapps
:: ##################################################################
:stable
if not exist %powerswitch% > %powerswitch% echo Turning On The Power. && >> %state% echo Turning On The Power @ %date% %time%
if exist SayStatic.exe SayStatic SERVER ANNOUNCMENT: THE SERVER IS NOMINAL.
echo SERVER STABLE.
sleep 7
cls
goto startapp
echo.
:: @@@@@@@@@@@@@@@@@@@@@@@@@@ S TURN OFF APPS @@@@@@@@@@@@@@@@@@@@@@@
:endapps
set Clients=CCleaner.exe
Call :rest
set Clients=Updater.exe
Call :rest
set Clients=test3.exe
Call :rest
set Clients=test4.exe
Call :rest
:: goto endloop
exit /b |:: (or whatever else you want to do)
del clien*.txt
EndLocal
:: @@@@@@@@@@@@@@@@@@@@@@@@@@ M TURN OFF APPS @@@@@@@@@@@@@@@@@@@@@@@
:rest
set apps=0
set var=0
:turnoff
for /f "tokens=*" %%1 in ('tasklist /nh ^|find "%clients%"') do (
for /f "tokens=5" %%A in ("%%1") do (
set apps=%%A
)
)

> task.txt echo %clients%
if not "%apps%"=="0" echo %apps% %%A &&>> client.txt echo ACTIVE %clients%
if not "%apps%"=="0" if not exist %powerswitch% call endapp
if "%apps%"=="0" echo %clients% is not running.
del task.txt
if exist client.txt del client.txt
sleep 6
cls
:endloop
:: @@@@@@@@@@@@@@@@@@@@@@@@@@ E TURN OFF APPS @@@@@@@@@@@@@@@@@@@@@@@
:: ------------------------------------------------------------------
:startapp
:: @@@@@@@@@@@@@@@@@@@@@@@@@@ S @TURN ON APPS @@@@@@@@@@@@@@@@@@@@@@@
:: UNDER CONSTRUCTIONS HERE
:: @@@@@@@@@@@@@@@@@@@@@@@@@@ E @TURN ON APPS @@@@@@@@@@@@@@@@@@@@@@@
:end
cls


Report •

Related Solutions

#4
January 26, 2012 at 15:58:59
Thanks for that Michelle.

On spending a bit more time on your original script I seem to have misinformed you in my previous post. Looking at this part of your script:

    pause
    del task.txt
    if exist client.txt del client.txt
    sleep 6
    cls
    :endloop
    cls

    :: @@@@@@@@@@@@@@@@ E TURN OFF APPS @@@@@@@@@@@@@@@@
    :setvari
    for /L %%i in (1 1 4) do (
    if "%%i"=="1" set clients=CCleaner.exe && goto turnoff
    if "%%i"=="2" set clients=test1.exe && goto turnoff
    if "%%i"=="3" set clients=test2.exe && goto turnoff
    if "%%i"=="4" set clients=winamp.exe && goto endloop
    )

    :endloop

I now note that there is no command line between Pause and Setvari which will redirect execution so that the Setveri For loop is started anew time after time so this is what caused the looping you experienced, not that the For /L loop was executed just once.

Anyway I'm glad to have been some help. Thanks for coming back to report your success.

Wahine.


Please come back & tell us if your problem is resolved.


Report •

#5
January 26, 2012 at 16:28:34
Thanks Wahine, well your code works which is all that matters, I am very new with FOR loops still learning a lot though from people such as yourself. I have quite a lot of batch files now all doing different things, what I am trying to do now is make them into MODULES so each does a specific thing to try and tidy things up a bit.

Again Thanks, Michelle xxxx


Report •

Ask Question