Redirecting command output to environment variable and tracking errorlevel

Discussion in 'Microsoft Windows 2000 CMD Promt' started by yairinbal@yahoo.com, Feb 22, 2007.

  1. Guest

    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
     
    , Feb 22, 2007
    #1
    1. Advertisements

  2. foxidrive Guest

    On 22 Feb 2007 05:10:28 -0800, 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
     
    foxidrive, Feb 22, 2007
    #2
    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






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


    Jerold Schulman
    Windows Server MVP
    JSI, Inc.
    http://www.jsiinc.com
     
    Jerold Schulman, Feb 22, 2007
    #3
  4. Tom Lavedas Guest

    On Feb 22, 8:10 am, 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
     
    Tom Lavedas, Feb 22, 2007
    #4
  5. Guest

    On Feb 22, 4:24 pm, "Tom Lavedas" <> wrote:
    > On Feb 22, 8:10 am, 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
     
    , Feb 22, 2007
    #5
  6. Todd Vargo Guest

    wrote:
    > On Feb 22, 4:24 pm, "Tom Lavedas" <> wrote:
    > > 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%


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

    --
    Todd Vargo
    (Post questions to group only. Remove "z" to email personal messages)
     
    Todd Vargo, Feb 23, 2007
    #6
  7. <> wrote in message
    news:...
    > On Feb 22, 4:24 pm, "Tom Lavedas" <> wrote:
    > > On Feb 22, 8:10 am, 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...


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

    On Tue, 27 Feb 2007 22:40:45 -0700, "Al Dunbar [MS-MVP]"
    <> wrote:

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


    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
    #8
    1. Advertisements

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.
Similar Threads
  1. Guest

    Set environment variable = output of exe

    Guest, Oct 7, 2003, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    4
    Views:
    1,978
    Bill Stewart
    Oct 7, 2003
  2. Babbit

    is this computer on the domain? Return %errorlevel%

    Babbit, Nov 11, 2003, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    4
    Views:
    426
    Jerold Schulman
    Nov 12, 2003
  3. Keith Hill

    Global exit on ERRORLEVEL 1?

    Keith Hill, Dec 15, 2003, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    9
    Views:
    1,512
    Al Dunbar [MS-MVP]
    Dec 17, 2003
  4. GrayB

    ERRORLEVEL in FOR?

    GrayB, Dec 18, 2003, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    6
    Views:
    1,564
    GrayB
    Dec 27, 2003
  5. Claus Koch

    Pipe Output into Environment Variable?

    Claus Koch, Aug 11, 2004, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    1
    Views:
    14,455
    Matthias Tacke
    Aug 11, 2004
  6. murdo

    Environment variable inside an environment variable

    murdo, Feb 24, 2005, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    7
    Views:
    3,454
    Mark V
    Feb 25, 2005
  7. ghquick

    errorlevel not set in for loop - delayed expansion !errorlevel! used

    ghquick, Jul 5, 2006, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    3
    Views:
    3,070
    Timo Salmi
    Jul 7, 2006
  8. Praetorian Guard

    For Loop and ErrorLevel

    Praetorian Guard, Jan 20, 2009, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    19
    Views:
    3,706
    Rich Pasco
    Mar 12, 2009
Loading...