Re: Redirecting command output to environment variable and tracking errorlevel



On Feb 22, 4:24 pm, "Tom Lavedas" <tglba...@xxxxxxx> wrote:
On Feb 22, 8:10 am, yairin...@xxxxxxxxx wrote:





Hello,

I am trying to run a command, fetch its output to an environment
variable and get errorlevel. I know I can solve it with simple exe/
perl that do it, but I need to stick to CMD

I first tried:

for /f "delims=" %%A in ('mycmd') do set Output=%%A
echo %errorlevel%
echo %Output%

The output works fine but the ErrorLevel is of the set command and not
of mycmd.

Then I tried

for /f "delims=" %%A in ('mycmd') do set Rc=%errorlevel% & set Output=%
%A
echo %Rc%
echo %Output%

But the cmd shell substiture error level before running the command.

The next step was using delayed expansion:

setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%A in ('mycmd') do set Rc=!errorlevel! & set Output=%
%A
echo %Rc%
echo %Output%

but still I've got the errorlevel value of before the execution of CMD

Does anyone have any ideas?

Thanks,
Yair

This is the only way I can think to do what you want - at least it is
one way ...

mycmd > %temp%\out.txt
set RC=%Errorlevel%
for /f "delims=" %%a in (%temp%\out.txt) do set Output=%%a
del %temp%\out.txt
echo %Rc%
echo %Output%

Tom Lavedas
=============http://members.cox.net/tglbatch/wsh- Hide quoted text -

- Show quoted text -

Hello,

I there a way doing it without using temporary files. The output of
mycmd is sensitive and I would like it to be stored on files?
It looks like a simple requirement to catch an output and return code
from a cmd but appearently it doesnt... only one line in perl...

Thanks,
Yair

.



Relevant Pages


Loading