Home > other >  batch script: The process tried to write to a nonexistent pipe
batch script: The process tried to write to a nonexistent pipe

Time:03-29

@echo off
(for /f "skip=1 tokens=3,5,11" %%a in (Data.txt) do (if /i "%%c"=="%1" (if %%b==%2 echo %%a))
echo EXIT
)|Sum.exe

I'm trying to write a simple batch script that would take the .txt file with columns of data (Data.txt), find some values using 'if' and redirect all found values to stdin input of "Sum.exe".

"EXIT" is also redirected as it means that there's no more input to be given.

When I run above code first found value is printed in console and then "The process tried to write to a nonexistent pipe" message error is printed multiple times. Therefore echo EXIT somehow must be messing up with |Sum.exe. How to properly redirect both for and echo Exit into Sum?

EDIT:

Ok, so here's the input part of the Sum program (written in c )

std::string a;
while (a != "EXIT")
{
    std::cin >> a;
    if (isNumber(a))
    add(sum, std::stoi(a));
}

I added cout to see whether the data was being processed and it seems that the commands in batch script were treted as input aswell.

CodePudding user response:

My first suggestion would be procedure call: call :PROCEDURE. But the call doesn't work with pipe redirection. For example call :PROCEDURE | SORT return error: Invalid attempt to call batch label outside of batch script.

So I suggest to use input parameter as switch flag to call self batch. For example it would be third parameter: %3. Batch calls itself while third parameter equals x.

So I made this code

@echo off

if "%3"=="x" (%~nx0  %1 %2|sort )& GOTO :EOF

for /f "skip=1 tokens=3,5,11" %%a in (Data.txt) do (
if /i "%%c"=="%1" (if %%b==%2 echo %%a)
echo EXIT
)

I use sort.exe utility in my example. Change it to your sum.exe To call batch-file use syntax: my_batch.cmd [value11] [value5] x

P.S. I think you do not need word EXIT in output but I left it in example code.

  • Related