Redirecting command output to environment variable and tracking errorlevel

Discussion in 'Microsoft Windows 2000 CMD Promt' started by, Feb 22, 2007.

  1. Guest


    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=%
    echo %Rc%
    echo %Output%

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

    The next step was using delayed expansion:

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

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

    Does anyone have any ideas?

    , Feb 22, 2007
    1. Advertisements

  2. foxidrive Guest


    @echo off
    echo %errorlevel%
    for /f "delims=" %%a in ('file.txt') do set Output=%%a
    del file.txt
    foxidrive, Feb 22, 2007
    1. Advertisements

  3. 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

    Jerold Schulman
    Windows Server MVP
    JSI, Inc.
    Jerold Schulman, Feb 22, 2007
  4. Tom Lavedas Guest

    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
    Tom Lavedas, Feb 22, 2007
  5. Guest


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

    , Feb 22, 2007
  6. Todd Vargo Guest

    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.
    Todd Vargo, Feb 23, 2007
  7. 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 Dunbar [MS-MVP], Feb 28, 2007
  8. foxidrive Guest

    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.
    foxidrive, Feb 28, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.