0

I wanted to make a batch script for killing a process with some process id. I scripted a few cmd commands as:

echo " So what is the process id that is blocking tcp.."
set /p prcs_id="Enter the process id of the process you want to terminate :"
pause 
echo I got the process id: %prcs_id%
pause
taskkill /PID %prcs_id% /F

But the taskkill code gives me the error as such :

ERROR: Invalid syntax. Value expected for /PID

What might be the possible cause for this- since when I run the command as it is in cmd I get no error...

11
  • 2
    Please, don't post images of texts - it is harder to read... Commented Feb 7, 2018 at 19:17
  • 1
    Post the actual code (all relevant to reproduce) so we may better assist you in fixing it please. Commented Feb 7, 2018 at 19:19
  • I recommend turning echo on, so that you can see exactly the command that is giving you the syntax error. I think something unexpected is ending up in %prcs_id%. I tried your code, and did not have a problem. Commented Feb 7, 2018 at 19:25
  • @abelenky when I turned to get the echo %prcs_id% just after the set /p procs_id command the result was blank. Now what should I go with? Commented Feb 7, 2018 at 20:03
  • 1
    I fixed it for you. Do not ever post images like that again. Do not post code in comments. Commented Feb 7, 2018 at 21:27

2 Answers 2

1

I recommend adding a line in your file

set /P prcs_id="Enter a process id : "
echo I got process ID: %prcs_id%
taskkill /PID %prcs_id% /F

See if the prcs_id is exactly what you think you entered.

I suspect that for some reason, it is not quite what you think you typed in?


I'm not entirely sure, but this may be the delayed-expansion problem. Near the top of your batch file, add:

setlocal enabledelayedexpansion

Then, change your taskkill line to be:

taskkill /PID !prcs_id! /F

(use ! instead of %)

Sign up to request clarification or add additional context in comments.

1 Comment

I am getting the prcs_id as blank. When I tried on using your code. Now what might be the error..?
0

Try this one.

@ECHO OFF
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit)
SET /P MyPID=Please enter your PID:
IF "%MyPID%"=="" GOTO Error
ECHO This %MyPID% PID will terminated
GOTO End
:Error
ECHO You did not enter PID
:End
pause
taskkill /PID %MyPID% /t

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.