Capturing program output in a variable
Results 1 to 2 of 2

Thread: Capturing program output in a variable

  1. #1
    Registered User CeeBee's Avatar
    Join Date
    Nov 2002
    Location
    USA
    Posts
    2,494

    Capturing program output in a variable

    A quick one to warm up some brains:
    I have a program that outputs to stdout. Is there any simple way to capture that output in a variable? Only the 1st word matters. I've been thinking of this
    but I want something much easier if possible. Should work under Win9x-NT-2K-XP

    myprogram.exe [|arguments]>temp.var
    for %%i in (temp.var) do set myvariable=%%i
    del temp.var
    call nextstep.bat %myvariable%

    Also any ideea how to get the computer name and logged on username in Win95? %computername% and %username% don't exist there.

    Auditing time has come here too
    Ideeas? I smell smoke already
    Protected by Glock. Don't mess with me!

  2. #2
    Registered User
    Join Date
    Dec 1999
    Location
    Columbus Ohio U.S.A.
    Posts
    194
    ftp://garbo.uwasa.fi/pc/batchutil/dmgterr.zip may work for you.

    this is what I as actually looking for...

    All rights reserved Copyright (c) 1993-2003 by Timo Salmi

    From [email protected] Sat Dec 27 01:00:24 2003
    Subject: Testing for the errorlevel
    Date: Sat, 27 Dec 2003 01:00:24
    From: [email protected] (Timo Salmi)

    23. Alternatives for testing for the errorlevel value
    ================================================== ===

    Many programs and some MS-DOS commands (like diskcomp, format and
    xcopy) return an errorlevel exit code on termination. Testing for
    the errorlevel is complicated by the cumulative nature of
    errorlevels. Thus if you wish to test if the errorlevel was
    (exactly) 2, you must use
    if errorlevel==2 if not errorlevel==3 echo Errorlevel 2
    Another alternative is utilizing the for command:
    for %%e in (0 1 2 3 4 5 6 7) do if errorlevel==%%e set _errlev=%%e
    if "%_errlev%"=="2" echo Errorlevel 2
    Alternatively, and more generally
    for %%e in (0 1 2 3 4 5 6 7) do if errorlevel==%%e set _errlev=%%e
    if "%_errlev%"=="2" echo Errorlevel %_errlev%
    A convenient trick in more complicated batches is using the goto
    command:
    for %%e in (0 1 2) do if errorlevel==%%e goto _label%%e
    goto _out
    :_label0
    echo Errorlevel 0
    :_label1
    echo Errorlevel 1
    :_label2
    echo Errorlevel 2
    :_out
    See BOOT.BAT for actual usage of this technique.
    The order in which there errorlevels are given is significant,
    since all the errorlevels up to the one returned will be on.
    Consider the following demonstration
    @echo off
    find "hello" con > nul
    if errorlevel==2 echo errorlevel 2 % error in search %
    if errorlevel==1 echo errorlevel 1 % no match found %
    if errorlevel==0 echo errorlevel 0 % match found %
    If you type
    peekaboo
    ^Z
    you'll get *both*
    errorlevel 1
    errorlevel 0
    If you type
    hello
    ^Z
    then you'll get just
    errorlevel 0

    If you are ready allow a few additional lines for a more clear
    documentation and easier to understand logic, consider this example
    of choice usage
    @echo off
    choice /tq,5 /cynq
    for %%i in (0 1 2 3 255) do if errorlevel==%%i set i_=%%i
    if "%i_%"=="0" echo You effected a break
    if "%i_%"=="1" echo Your input was Yes
    if "%i_%"=="2" echo Your input was No
    if "%i_%"=="3" echo Your input was Quit
    if "%i_%"=="255" echo Error condition
    set i_=

    A more complicated example
    @echo off
    if "%2"=="recurse" goto _for
    choice /s/c:123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm nopqrstuvwxyz
    for %%j in (0 1 2 3 4 5 6) do call %0 %%j recurse
    goto _level
    :_for
    for %%i in (%10 %11 %12 %13 %14 %15 %16 %17 %18 %19 255) do if errorlevel==%%i set i_=%%i
    goto _exit
    ::
    :_level
    echo errorlevel==%i_%
    :_exit

    Speaking of errorlevels, some novice batch users occasionally ask
    for the general list of the meanings of the errorlevel values. This
    question is based on a failure to grasp how errorlevels are created.
    Batch files can only test their values. It is the executable
    programs that can return errorlevel values. What the particular
    values are, which an executable program returns, is totally
    dependent on the individual program. Some programs may be able to
    return a lot of different values, some none (or rather return zero
    errorlevel). Perhaps this misunderstanding is based on the fact that
    MS-DOS includes a number of programs, such as CHOICE.COM, FIND.EXE
    and XCOPY.EXE which return errorlevels. For example MS-DOS version
    6.22 XCOPY.EXE can signal with six different errorlevel values (0 to
    5) depending on the outcome. For those values see "HELP XCOPY" and
    select "Notes".

    As explained, the errorlevel returned depends on the program called.
    But if you wish to set a specific errorlevel within a batch file
    (e.g. for testing purposes), you can use G(nu)AWK exit as follows
    within your batch file
    echo.|gawk '{exit N}'
    where you can put as N anything between 0 and 255. Alternatively,
    just
    gawk 'BEGIN{exit N}'
    --------------------------------------------------------------------
    Last edited by xt477; March 9th, 2004 at 09:15 AM.
    "I may not like what you have to say, but I will defend to the death your right to say it" Voltaire.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •