Re: Redirecting command output to environment variable and tracking errorlevel



On 22 Feb 2007 05:10:28 -0800, yairinbal@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?

Untested

@echo off
mycmd>file.txt
echo %errorlevel%
for /f "delims=" %%a in ('file.txt') do set Output=%%a
del file.txt

.



Relevant Pages

  • Re: Redirecting command output to environment variable and tracking errorlevel
    ... variable and get errorlevel. ... echo %errorlevel% ... The output works fine but the ErrorLevel is of the set command and not ... But the cmd shell substiture error level before running the command. ...
    (microsoft.public.win2000.cmdprompt.admin)
  • Re: robocopy validation
    ... Robocopy did not copy any files. ... if errorlevel 8 echo **FAILED COPIES** & goto end ...
    (microsoft.public.win2000.general)
  • Re: simple batch script
    ... IF ERRORLEVEL number command ... ERRORLEVEL number Specifies a true condition if the last program run ... EXIST filename Specifies a true condition if the specified filename ... ELSE echo filename. ...
    (microsoft.public.win2000.cmdprompt.admin)
  • Re: Redirecting command output to environment variable and tracking errorlevel
    ... variable and get errorlevel. ... echo %errorlevel% ... The output works fine but the ErrorLevel is of the set command and not ... But the cmd shell substiture error level before running the command. ...
    (microsoft.public.win2000.cmdprompt.admin)
  • Redirecting command output to environment variable and tracking errorlevel
    ... variable and get errorlevel. ... echo %errorlevel% ... The output works fine but the ErrorLevel is of the set command and not ... But the cmd shell substiture error level before running the command. ...
    (microsoft.public.win2000.cmdprompt.admin)

Loading