1

I wrote this small script to kill Spotify whenever the title of the window is "Advertisement". For now, it just looks for the spotify.exe process and, if the name of the window matches, kills it (next step is executing it every second). However, I get an error every time I execute it, telling me that there's an unexpected ( in IF /i "%A:~0,4" (, but such statement is not in my code: it seems like Windows modifies IF /i "%%A:~0,4%"=="PID:" ( before executing it.

Here's the script:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

tasklist /fi "imagename eq spotify.exe" /fo list /v > tmp.txt

FOR /F "usebackq tokens=*" %%A IN ("tmp.txt") DO (
  SET test=%%A
  echo %%A
  IF /i "%%A:~0,4%"=="PID:" (
    SET "pid=%%A:PID:          =%"
    echo %pid%
  )
  IF /i "%%A:~0,13%"=="Window Title:" (
    SET "wintitle=%%A:Window Title: =%"
    echo %wintitle%
  )
  IF "%wintitle%"=="Advertisement" (
    taskkill /F /PID %pid%
  )
)

PAUSE

Error message (with echo on):

C:\Users\marco\Desktop>antispotify.bat

C:\Users\marco\Desktop>tasklist /fi "imagename eq spotify.exe" /fo list /v  1>tmp.txt
( was unexpected at this time.
C:\Users\marco\Desktop>  IF /i "%A:~0,4" (

Does anyone know what's wrong with my code?

  • 2
    substring substituiion with `for` variables doesn't work. Ise your `test` variable instead, together with [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) – Stephan Aug 31 '19 at 19:08

2 Answers2

1

As noted in the comments, substring manipulation doesn't work on for variables (%%a type). Instead, you need an ordinary variable and of course delayed expansion.

But may I suggest another approach:

@ECHO OFF
SETLOCAL 
for /f "tokens=2,9 delims=," %%a in ('tasklist /fi "imagename eq notepad.exe" /fo csv /v /nh') do (
   set "pid=%%~a"
   set "wintitle=%%~b"
)
set pid
set wintitle
IF "%wintitle%"=="Advertisement" taskkill /F /PID %pid%

Here we use the command directly with the for loop instead of using a temporary file. Besides that, we change the output format to csv (easier to parse) with no header line ( /nh)

( I used notepad.exe, because I don't have spotify, but that's easy to adapt)

Stephan
  • 47,723
  • 10
  • 50
  • 81
1

The task to force a real kill of Spotify process running for advertisement can be done also by using following batch file without using delayed expansion.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=1* delims=:" %%I in ('%SystemRoot%\System32\tasklist.exe /FI "imagename eq spotify.exe" /FO LIST /V 2^>nul') do (
    if /I "%%~I" == "PID" (
        for /F %%K in ("%%~J") do set "ProcessIdentifier=%%~K"
    ) else if /I "%%~I" == "Window Title" (
        for /F "tokens=*" %%K in ("%%~J") do if /I "%%~K" == "Advertisement" call %SystemRoot%\System32\taskkill.exe /F /PID %%ProcessIdentifier%%
    )
)
endlocal

The same code with using delayed environment variable expansion:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /F "tokens=1* delims=:" %%I in ('%SystemRoot%\System32\tasklist.exe /FI "imagename eq spotify.exe" /FO LIST /V 2^>nul') do (
    if /I "%%~I" == "PID" (
        for /F %%K in ("%%~J") do set "ProcessIdentifier=%%~K"
    ) else if /I "%%~I" == "Window Title" (
        for /F "tokens=*" %%K in ("%%~J") do if /I "%%~K" == "Advertisement" %SystemRoot%\System32\taskkill.exe /F /PID !ProcessIdentifier!
    )
)
endlocal

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • setlocal /?
  • taskkill /?
  • tasklist /?

See also: How does the Windows Command Interpreter (CMD.EXE) parse scripts?

Mofi
  • 38,783
  • 14
  • 62
  • 115