1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

directing output of exe to variable

Discussion in 'Support' started by jolly1, Apr 5, 2010.

  1. jolly1

    Joined:
    Apr 4, 2010
    Messages:
    5
    Likes Received:
    0
    Hi,

    I have managed to capture the output of program mediachanger.exe as follows

    for /f "usebackq delims=" %i in (`mediachanger unmount`) do set changer_reply=%i

    unfortunately some of the output from mediachanger is over multiple lines as follows

    000: full
    001: full
    002: full
    003: full
    004: full
    005: full
    006: full
    007: full

    I would like to red each line into a different variables

    eg n1 = 000: full
    n2 = 001: full

    Have been unable to do this. I would be gratefull for any help

    Oh by the way I have very little expierence writing code
     
  2. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,972
    Likes Received:
    30
    Not tested much but has some basic functionality:

    Code:
    v:\> set j=0 & for /f "usebackq delims=" %i in (`echo foo^r^nbar^r^ndog^r^ncat`) do (set line%j=%i & set j=%@inc[%j])
    
    v:\> set line*
    line0=foo
    line1=bar
    line2=dog
    line3=cat
    It looks better in a batch file using DO.

    Code:
    v:\> type dodo.bat
    set i=0
    do line in /p ping ilucky
            iff "%line" NE "" then
                    set line%i=%line
                    set i=%@inc[%i]
            endiff
    enddo
    
    v:\> dodo.bat
    
    v:\> set line*
    line=
    line0=Pinging lucky.syr.edu [128.230.13.36] with 32 bytes of data:
    line1=Reply from 128.230.13.36: bytes=32 time=43ms TTL=117
    line2=Reply from 128.230.13.36: bytes=32 time=41ms TTL=117
    line3=Reply from 128.230.13.36: bytes=32 time=41ms TTL=117
    line4=Reply from 128.230.13.36: bytes=32 time=43ms TTL=117
    line5=Ping statistics for 128.230.13.36:
    line6=    Packets: Sent = 4, Received = 4, Lost = 0 (0 loss),
    line7=Approximate round trip times in milli-seconds:
    line8=    Minimum = 41ms, Maximum = 43ms, Average = 42ms
     
  3. jolly1

    Joined:
    Apr 4, 2010
    Messages:
    5
    Likes Received:
    0
    Vince thanks for the reply bit I tried dodo.bat and it returns no output only get
    c:\batch\dodo.bat
    set i=0
    do line in /p ping ilucky


    I tried ping ilucky and do get an output from this command
     
  4. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,972
    Likes Received:
    30
    I doubt "ping ilucky" would return useful results for you since "ilucky" is an
    alias. If it's returning error messages (stderr) you won't capture them. Try
    pinging a **real** computer (like lucky.syr.edu).

    On Tue, 06 Apr 2010 08:37:22 -0400, jolly1 <> wrote:

    |Vince thanks for the reply bit I tried dodo.bat and it returns no output only get
    |c:\batch\dodo.bat
    |set i=0
    |do line in /p ping ilucky
    |
    |
    |I tried ping ilucky and do get an output from this command
    |
    |
    |
    |
    --
    - Vince
     
  5. jolly1

    Joined:
    Apr 4, 2010
    Messages:
    5
    Likes Received:
    0
    Vince still no joy

    here is the file

    set i=0
    do line in /p ping lucky.syr.edu
    iff "%line" NE "" then
    set line%i=%line
    set i=%@inc[%i]

    endiff
    enddo
    exit

    here is the output
    set i=0
    do line in /p ping lucky.syr.edu
    exit

    tried as dodo.bat and dodo.btm same result
     
  6. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,312
    Likes Received:
    39
    Note that PING's output contains redirection characters, so it probably isn't the best utility to test with.... For starters, you could change

    Code:
    set line%i=%line
    to

    Code:
    set line%i="%line"
    to better handle the less-than signs. You should also remove the EXIT line; that exits the shell, losing all the environment variables you have just created.
     
  7. Glennbo51

    Glennbo51 Guest

    On 04/06/2010 10:21, jolly1 wrote:

    This is the output environment variables I got when when I ran the above
    BAT files

    lineline0=Pinging lucky.syr.edu [128.230.13.36] with 32 bytes of data:
    line1=Reply from 128.230.13.36: bytes=32 time=71ms TTL=118
    line2=Reply from 128.230.13.36: bytes=32 time=74ms TTL=118
    line3=Reply from 128.230.13.36: bytes=32 time=77ms TTL=118
    line4=Reply from 128.230.13.36: bytes=32 time=74ms TTL=118
    line5=Ping statistics for 128.230.13.36:
    line6= Packets: Sent = 4, Received = 4, Lost = 0 (0 loss),
    line7=Approximate round trip times in milli-seconds:
    line8= Minimum = 71ms, Maximum = 77ms, Average = 74ms

    Is that what you expected? Nothing output to screen though
     
  8. jolly1

    Joined:
    Apr 4, 2010
    Messages:
    5
    Likes Received:
    0
    Hi success at last, when the last post indicating all Ok. Tried again.

    and success

    @echo off
    set i=0
    do line in /p mediachanger fullslots
    iff "%line" NE "" then
    set line%i="%line"
    set i=%@inc[%i]

    endiff
    enddo
    echo %line1%
    echo %line2%
    echo %line3%
    echo %line4%
    echo %line5%
    echo %line6%
    echo %line7%
    echo %line8%

    this time all variables listed output

    thanks a lot for all the help
     
  9. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    | @echo off
    | set i=0
    | do line in /p mediachanger fullslots
    | iff "%line" NE "" then
    | set line%i="%line"
    | set i=%@inc[%i]
    |
    | endiff
    | enddo
    | echo %line1%
    | echo %line2%
    | echo %line3%
    | echo %line4%
    | echo %line5%
    | echo %line6%
    | echo %line7%
    | echo %line8%

    Here is a simpler version (and safer, too - leftover variables are deleted
    first):

    --- begin code ---
    @echo off
    setlocal
    unset i line*
    do line in /p mediachanger fullslots
    iff defined line then
    set i=%@inc[%i]
    set line%i=%@safeenv[%line]
    endiff
    enddo
    for /l %n in (1,1,%i) echo %@format[3,%n] %[line%n]
    --- end code ---

    This utilizes the TCC feature that the arithmetic value of an undefined
    variable is 0, and the @SAFEENV function from Charles Dye's plugin
    SAFECHARS.DLL, which converts characters significant to the parser to
    characters with the same visible representation but not affecting the
    parser.
    --
    Steve
     
  10. jolly1

    Joined:
    Apr 4, 2010
    Messages:
    5
    Likes Received:
    0
    Steve where do I download safechars.dll I looked in the plugins a don't see it
     
  11. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,191
    Likes Received:
    11
    Why can't you use the new @execarray or @filearray functions?

    unsetarray /q line
    mediachanger fullslots > foo.$$$
    setarray line[%@inc[%@lines[foo.$$$]]]
    set num_slots=%@filearray[line,foo.$$$]
    del /q foo.$$$
    do i = 0 to %num_slots
    echo "%line[%i]"
    enddo

    Or alternatively, if you know how many lines there are:

    unsetarray /q line
    setarray line[200]
    echo %@execarray[line,mediachanger fullslots] >& nul
    echo %line[0]
    echo %line[1]
    etc.

    The caveat is that array functions and variables are not available in the
    TCC/LE version.

    -Scott

    Steve F畸i疣 <> wrote on 04/06/2010 04:59:28 PM:


    deleted

     

Share This Page