directing output of exe to variable

Apr 4, 2010
5
0
#1
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
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
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
 
Apr 4, 2010
5
0
#3
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
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
 
Apr 4, 2010
5
0
#5
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
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,650
46
Albuquerque, NM
prospero.unm.edu
#6
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
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.
 
G

Glennbo51

Guest
#7
On 04/06/2010 10:21, jolly1 wrote:

> 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
>
>
>
>
>
>
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
 
Apr 4, 2010
5
0
#8
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
| @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
 

samintz

Scott Mintz
May 20, 2008
1,295
11
Solon, OH, USA
#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$BaC(Bi$BaO(B <> wrote on 04/06/2010 04:59:28 PM:


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