Discussion:
Returning a value from a random batch thru CMD.EXE
(too old to reply)
R.Wieser
2024-04-03 16:51:22 UTC
Permalink
Hello all,

I've got a program which does a "CMD /C {batchfile}", and I would like to
receive a value the batchfile exists with.

Not a problem, I just do an "exit {value}".

But I now have a few batchfiles, some of which could call another - one
which I can also execute directly. And that creates a problem :

When I run the batchfile directly the above "exit {value}" will propagate
down thru CMD.EXE into the program and I can see the {value}.

But when I call another batchfile which calls the above batchfile (as a
subroutine) that latter one will not return control to the former one, but
instead just terminates CMD.EXE. :-(


I found that I could use "exit /b {value}" (causing the called bachfile to
return control to the caller), but when used in the main batchfile that
{value} will not propagate down thru CMD.EXE


Question:
How do I tell CMD.EXE to accept the last "exit /b {value}" and return it as
its own exit value ?

Remark: I think I already have a *work-around* figured out, but if there is
a cleaner way to do it (replacing the "/b" argument with something else ?) I
would rather use that.

Regards,
Rudy Wieser
Zaidy036
2024-04-04 16:10:32 UTC
Permalink
Post by R.Wieser
Hello all,
I've got a program which does a "CMD /C {batchfile}", and I would like to
receive a value the batchfile exists with.
Not a problem, I just do an "exit {value}".
But I now have a few batchfiles, some of which could call another - one
When I run the batchfile directly the above "exit {value}" will propagate
down thru CMD.EXE into the program and I can see the {value}.
But when I call another batchfile which calls the above batchfile (as a
subroutine) that latter one will not return control to the former one, but
instead just terminates CMD.EXE. :-(
I found that I could use "exit /b {value}" (causing the called bachfile to
return control to the caller), but when used in the main batchfile that
{value} will not propagate down thru CMD.EXE
How do I tell CMD.EXE to accept the last "exit /b {value}" and return it as
its own exit value ?
Remark: I think I already have a *work-around* figured out, but if there is
a cleaner way to do it (replacing the "/b" argument with something else ?) I
would rather use that.
Regards,
Rudy Wieser
One way is to use SETX which provides an environmental variable for
another batch to read.
R.Wieser
2024-04-04 17:01:05 UTC
Permalink
Zaidy036,
Post by Zaidy036
One way is to use SETX which provides an environmental variable for
another batch to read.
Ahhh... no. That command writes into the top-level user / system
environment, and thereby makes that variable evailable to *all* programs
that are started *after* it.

There are a few problems with that :

1) I only need the process which started the batchfile to receive the
changes.

2) As my process has been started before running SETX it will not receive
the changes.

And there is the likelyhood that putting a few what /should/ be local to my
process environment variables there could cause problems for/with other
programs. Including possibly overwriting a variable which I should leave
alone.

Thanks for the suggestion though.

Regards,
Rudy Wieser

Loading...