Arush Agarampur
Arush Agarampur

Reputation: 1430

How can I redirect standard output to a program in CMD?

I am running this in the command prompt:

python -c ""print("""Message from python""")"" | AcceptMessage.exe

However, this does not work. I get the message:

close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr

AcceptMessage.exe takes in one argument. All it does is just print out what was passed to it. Meaning, if you normally call it via AcceptArgument.exe argument_here, the output would be argument_here. I've tested the actual program, and it works fine normally, so that's not the problem.

I'm pretty new to scripting in general, so I'd appreciate any help.

Upvotes: 1

Views: 78

Answers (1)

npocmaka
npocmaka

Reputation: 57322

try like this (save the script with .bat extension)

0<0# : ^
''' 
@echo off

for /f "tokens=* delims="  %%a in ('python "%~f0" %*') do (
    AcceptMessage "%%a"
)
exit /b 0
'''

print("message from python")

or like that:

@echo off

for /f "tokens=* delims=" %%a in ('"python -c ""print("""Message from python""")"""') do (
    acceptMessage "%%a"
)

if you want to run it directly from the command line:

for /f "tokens=* delims=" %a in ('"python -c ""print("""Message from python""")"""') do @AcceptMessage "%a"

Upvotes: 1

Related Questions