samedi 31 août 2019

Batch error "( unexpected" if statement nested inside for loop

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?

Aucun commentaire:

Enregistrer un commentaire