How to pass file content as a command argument in Windows cmd?
Asked Answered
F

2

8

I want to do something like bash

kill -9 `cat pid.txt`

in Windows. I tried

set /p mypid= < pid.txt && taskkill /f /pid %mypid%

but it doesn't work, looks like %mypid% becomes assigned after executing of taskkill.

How to do it? (I'm going to call this command from NSIS install script, so I don't want to make a separate .bat file for it).

Fisken answered 18/6, 2013 at 15:51 Comment(1)
The Windows platform lacks several mechanisms common on the Linux/Unix world. You could try to fragment the process: 1) read the pid file and 2) pass it to taskkillAiguillette
V
9

I'm not sure what the contents of pid.txt are, but assuming it's just the PID by itself, you could do this:

for /f %%x in (pid.txt) do taskkill /f /pid %%x

If you're running that from the command line, and not as part of a batch file, replace the %%x with %x.

If that's not what is in pid.txt, please edit your question to be clearer on that point.

Visby answered 18/6, 2013 at 20:28 Comment(4)
Thank you. The pid.txt is just PID itself. But when I tried to run it, Windows just executed notepad with pid.txt for me.Fisken
Outstanding! I had no idea Windows batch files could do that! Thanks.Cryolite
It only generate an error for me : %%x was unnexpectedFebrifacient
If you're running that directly from the command line but NOT in a batch file, use %x rather than %%x.Visby
A
1

The following script is doing what you expect, without depending on a shell feature:

OutFile "kill.exe"

Var pid
!define filename "pid.txt"

Section

    ClearErrors
    FileOpen $0 "${filename}" r
    IfErrors 0 readok
    DetailPrint "Error to open ${filename}"
    goto done
    readok:
    FileRead $0 $pid
    DetailPrint "pid found=$pid"
    FileClose $0

    ExecWait 'taskkill /F /PID $pid'
    done:

SectionEnd
Aiguillette answered 19/6, 2013 at 10:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.