2

I would like to be able to manually type an 'alias' keyword into the command console and have it execute the following command a specified number of times.

taskkill /PID "%PID%" /f >nul 2>&1 

I found this command which will run a loop:

for /l %x in (1, 1, 5) do

It works fine for simple commands like echoing text, but when I run the following (without the >nul 2>&1 just for the purposes of this test)

for /l %x in (1, 1, 5) do taskkill /PID "%PID%" /f 

it stops after one time (displaying a 'success' message) instead of repeating the same command a further 4 times.

How can I get the command to execute multiple times without it stopping after each instance?

And once that's been achieved, how can I run that command from an alias?

I understand that certain characters will need to be escaped when the command is put into a batch file

set "close=taskkill /PID "%PID%" /f >nul 2>&1" doskey close=taskkill /PID "%PID%" /f ^>nul 2^>^&1 

and that double percentage character will be needed for %x loop command, but I still don't know what syntax to use to run the looping command when a specified 'alias' keyword is typed.

2
  • 2
    You're killing a specific process with a specific process ID with taskkill /PID "%PID%". The odds of a new process with the same process ID that you just killed being created immediately after killing the first one are practically zero. Commented Nov 14, 2018 at 2:39
  • The FOR /L example you are using definitely executes 5 times on my computer. Commented Nov 14, 2018 at 2:42

1 Answer 1

1

From a commandline:

Note: > is at start of line to represent the prompt.

>doskey close=for /l %x in (1, 1, 5) do taskkill /pid ^%pid^% /f >set "pid=10" >close 

is to set a doskey macro to use %pid%. Set pid to a value i.e 10 and then execute the macro.

>doskey close=for /l %x in (1, 1, 5) do taskkill /pid $1 /f >close 10 

is to set a doskey macro to use 1st argument as pid. Execute the macro using argument i.e. 10.


From a batch file to set doskey:

doskey close=for /l %%x in (1, 1, 5) do taskkill /pid %%pid%% /f 

or set as an argument to pass the pid.

doskey close=for /l %%x in (1, 1, 5) do taskkill /pid $1 /f 

Execute the macroes like the 1st 2 commandline examples.


The command in the macro will run 5 times. Running the macro is shown in commandline examples.

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

3 Comments

Thanks very much. Unfortunately it's still not working. If I've understood you correctly, one of your example uses %PID% to store the number of times the loop should be run. However, I have other code that determines the relevant process ID and stores it in that %PID% variable. I therefore used the example where an argument was set to pass the %PID%. The result was the same as if I just ran the original command (i.e. I get a Success message that the process has been terminated). But it happens just the once. Perhaps I should elaborate on my goal (see next comment).
I'm trying to create a "hacky" solution to close a console window and all its related processes but not any other console window. I spent ages looking for 'proper' solution but none forthcoming (hence this 'hacky' alternative) The original code I posted keeps finding relevant PID and killing it. If I manually paste the code in a few times, eventually the window will close when it reaches last related process (just same command over and over). Manually works fine, but when looped it keeps stopping. Perhaps the 'success' message resulting from the Taskkill command is stopping it repeating.
%PID% does not store the number of times the loop is run. The loop count is hard coded with (1, 1, 5) so %PID% has no effect on loop count. If %PID% is 10 and does not exist, I get echoed 5 times ERROR: The process "10" not found.. If running notepad as pid 2152, I get SUCCESS: The process with PID 2152 has been terminated. once, and ERROR: The process "2152" not found. 4 times. This is expected and your result seems odd. I get a message 5 times on Windows 7. I consider your issue unique and needs a more in depth review. Edit your question post if more details are available.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.