0

i have an issue with a dos code:

@echo off & set _c=

SET _list=calc.exe StikyNot.exe
echo %_list%
(for %%a in (%_list%) do (
:loop
tasklist /FI "IMAGENAME eq %%a" | find /i "%%a"
goto :_%errorlevel%
:_0
  ECHO process is running
  taskkill /IM calc.exe /F
  set _c=true
  Timeout /T 10
  goto loop
:_1
  IF "%_c%" == "true" (
    ECHO l'applicazione process has been killed.
    Timeout /T 10
    set _c=
    goto exitloop)
  ECHO process is not running
  Timeout /T 5 /Nobreak
  goto loop
:exitloop
)
)

the line

tasklist /FI "IMAGENAME eq %%a" | find /i "%%a"

does not accept the %%a parameters. I tried a bat test with the follow code:

ECHO OFF
SET _list=calc.exe StikyNot.exe

(for %%a in (%_list%) do (
   echo %%a
))

It print the list without any problem, so i think the issue is the tasklist parameters format. Can you help me?

Compo
  • 36,585
  • 5
  • 27
  • 39
  • the issue is `goto :_%errorlevel%`. See [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) (same with `IF "%_c%" == "true" (`) – Stephan Aug 12 '20 at 09:21
  • I've got a [deja vue](https://stackoverflow.com/questions/63357127/variable-not-recognized-in-else-statement-in-a-bat-file). given the similarity of the two questions: Is this some sort of homework? – Stephan Aug 12 '20 at 09:28
  • No, i want to create a script to stop some services on my pc on startup. – Andrea Mantovani Aug 12 '20 at 09:35
  • [**Never** use `:label` nor `:: label-like comment` inside a command block enclosed in `()` parentheses](https://stackoverflow.com/a/32147995/3439404). – JosefZ Aug 12 '20 at 11:43
  • 1
    If you don't want some services to start on startup, you should configure those services so that they aren't automatic, e.g. `%__AppDir__%sc.exe config MyServiceName start= demand`. This would be preferable to allowing them to start, and then trying to stop them. However, please note that your question seems to be about standard processes, not services. If you have processes starting at startup, you should try to remove the instruction which starts them, you should be able to do that in the System Configuration tool, _(msconfig)_. – Compo Aug 12 '20 at 12:16

0 Answers0