multi-line command output to single Var?

M

Mark V

multi-line command output to single Var?
(From CMD and not a batch) W2K

Through some SET tricks is it possible to capture a command's
STDOUT (multiple lines) using a one-liner FOR, appending each line
to a single variable?

I do not want to use an intermediate file, and this would have to
run as
CMD.EXE /c FOR ...

Example:

C:\temp4>subst
X:\: => D:\temp
Y:\: => C:\temp4
Z:\: => C:\temp6

C:\temp4>for /f "Tokens=1 Delims=:" %a in ('subst.exe') do echo/ %a
C:\temp4>echo/ X
X
C:\temp4>echo/ Y
Y
C:\temp4>echo/ Z
Z

Wanted:
SET ANS
ANS=XYZ
(or with delimiters if necessary,
ANS=$X$Y$Z )


ALTERNATIVE:
As above but all data on one single STDOUTline

Background: Trying to find a workaround from inside AutoIt3
for the apparent limitation of StdOutRead() only reading a single
output line. (rather noob w AU3 BTW)
And I do not want to write to disk at all.

Ultimate goal is to recover SUBST output into AU3. (sans file)
Or any other means of IDing Substituted drives on a system
from AU3.

Any insight would be appreciated.


PS Autoscript fora are Down at this time.
 
F

foxidrive

multi-line command output to single Var?
(From CMD and not a batch) W2K

Through some SET tricks is it possible to capture a command's
STDOUT (multiple lines) using a one-liner FOR, appending each line
to a single variable?

I do not want to use an intermediate file, and this would have to
run as
CMD.EXE /c FOR ...

Example:

C:\temp4>subst
X:\: => D:\temp
Y:\: => C:\temp4
Z:\: => C:\temp6

C:\temp4>for /f "Tokens=1 Delims=:" %a in ('subst.exe') do echo/ %a
C:\temp4>echo/ X
X
C:\temp4>echo/ Y
Y
C:\temp4>echo/ Z
Z

Wanted:
SET ANS
ANS=XYZ

Untested:

"%comspec%" /V:ON /c for /f "Tokens=1 Delims=:" %a in ('subst.exe') DO SET
ANS=!ANS!%a
 
M

Matthias Tacke

foxidrive said:
Untested:

"%comspec%" /V:ON /c for /f "Tokens=1 Delims=:" %a in ('subst.exe') DO SET
ANS=!ANS!%a

Hello foxidrive,

I've had that same idea, but vars in the new cmd will be lost upon return.

Maybe the trick with set /p =%a will do: (all on one line)

"%comspec%"
/V:ON /c for /f "Delims=:" %a in ('subst.exe') DO @echo/^|set /p =%a


I don't now autoit but there should be a better solution.

@MarkV AFAIK the for loop works also with (hidden) intermediate files.

HTH
Matthias
 
M

Mark V

multi-line command output to single Var?
(From CMD and not a batch) W2K

Through some SET tricks is it possible to capture a command's
STDOUT (multiple lines) using a one-liner FOR, appending each
line to a single variable?
[...]

Untested:

"%comspec%" /V:ON /c for /f "Tokens=1 Delims=:" %a in
('subst.exe') DO SET ANS=!ANS!%a
[ ]

Thanks! (resolved AutoIt3 problem in the mean time)
But still an interesting cmdline puzzle.

Your code seems to give me here
(Where U: X: Y: Z: are SUBST)

ANS=!ANS!UXYZ

Which would have been usable if I still needed it!
Thank you foxdrive!
 
M

Mark V

Hello foxidrive,

I've had that same idea, but vars in the new cmd will be lost
upon return.

Maybe the trick with set /p =%a will do: (all on one line)

"%comspec%"
/V:ON /c for /f "Delims=:" %a in ('subst.exe') DO @echo/^|set /p
=%a

From CMD with /V:ON and running the FOR part I see output:
|set /p=U
|set /p=X
|set /p=Y
|set /p=Z

(which would not have helped the perceived problem)
I don't now autoit but there should be a better solution.

There is. Incomplete documentation tripped this AU3 newbie up by
way of failing to mention that the RUN() function can push "lines"
of output onto a (unmentioned) hidden internal queue/stack.
StdOutRead() called in a loop can pop the FIFO lines and recover
all the original command's STDOUT.
In hindsight it all make perfect sense of course! LOL

@MarkV AFAIK the for loop works also with (hidden) intermediate
files.

Like the use of "invisible" pipes in some commands? Interesting.

Thanks guys for the ideas! Hope it was amusing at least. ;)
 
M

Matthias Tacke

Mark said:
From CMD with /V:ON and running the FOR part I see output:
|set /p=U
|set /p=X
|set /p=Y
|set /p=Z

(which would not have helped the perceived problem)
Yes, same here.
C:\test>for /f "Delims=:" %a in ('subst.exe') DO @echo/^|set /p
|set /p
|set /p

But this way it works for drives T and U (one line):

C:\test>cmd.exe /V:ON /c for /f "Delims=:" %a in ('subst.exe'
) DO @echo/^|set /p =%a
TU
C:\test>

Or this way:
C:\test>for /f "Delims=:" %a in ('subst.exe') DO @set /p =%a<nul
TU
C:\test>


BTW could you supply the autoit3 sample reading the output?

Thanks
Matthias
 
M

Mark V

Yes, same here.
C:\test>for /f "Delims=:" %a in ('subst.exe') DO @echo/^|set /p
|set /p
|set /p
But this way it works for drives T and U (one line):

C:\test>cmd.exe /V:ON /c for /f "Delims=:" %a in ('subst.exe'
) DO @echo/^|set /p =%a
TU
C:\test>

C:\TEMP>cmd.exe /V:ON /k
C:\TEMP>for /f "Delims=:" %a in ('subst.exe') DO @echo/^|set /p =%a
|set /p =N
|set /p =V
Here... W2K here BTW.
Or this way:
C:\test>for /f "Delims=:" %a in ('subst.exe') DO @set /p =%a<nul
TU
C:\test>

Ah ha! This one works here.
C:\TEMP>cmd.exe /V:ON /k
C:\TEMP>for /f "Delims=:" %a in ('subst.exe') DO @set /p =%a<nul
NV
BTW could you supply the autoit3 sample reading the output?

I see no "Reply-To: so assume the email is good?
I will send the test AU3 script I have.
(as TEST.ZIP (2,241b), MIME )
Subject will contain "Mark V"
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top