Redirecting command output to environment variable and tracking errorlevel

  • Thread starter Thread starter yairinbal
  • Start date Start date
Y

yairinbal

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
 
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?

Untested

@echo off
mycmd>file.txt
echo %errorlevel%
for /f "delims=" %%a in ('file.txt') do set Output=%%a
del file.txt
 
Try:
set err="%TEMP%\MyErr_%RANDOM%.err"
for /f "Tokens=*" %%A in ('mycmd') do (
@echo %ERRORLEVEL%>%err%
set Output=%%a
)
for /f "Tokens=*" %%A in ('type %err%') do set MyErr=%%A
@echo %Output%
@echo %MyErr






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

Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
 
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
 
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
 
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...

You could possibly set RC based on the output returned but you will need to
provide more details about what mycmd does, what output and respective
errorlevels it returns, etc. Without this information you are pretty much on
your own.
 
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...

Jerold's (multi-line) solution seems to avoid explicit use of files. But
beware! You can never really be sure that transitory information is not
indirectly stored in a file somewhere.

/Al
 
Jerold's (multi-line) solution seems to avoid explicit use of files. But
beware! You can never really be sure that transitory information is not
indirectly stored in a file somewhere.

From reading peoples comments, Al, I believe that pipes and commands in
for-in-do commands are stored in the registry while executing. Dunno if
that's in a RAM copy or on the HDD.

Of course it always been that dos/Win9x/ME store pipes etc in temporary
files in the %temp% location, if set.
 
Back
Top