EDit
Use the set /p prompt to output text without appending cr/lf like echo always does. So:
set /P =I love you<Nul|clip
won't append the cr/lf
Capture/parse output of a command
Suppose you want to get hostname from this ping output:
> ping -4 -n 1 -a 127.0.0.1
Pinging Matar [127.0.0.1] with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Ping statistics for 127.0.0.1:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
This batch
For /f "tokens=2delims=[ " %%A in (
'ping -n 1 -a 127.0.0.1^|find "["'
) do Echo %%A
Will return
Matar
This batch will store all non empty output lines to a pseudo array var and list the vars
@Echo off & Setlocal EnableDelayedExpansion
Set Cnt=100
For /f "delims=" %%A in ('Ping -4 -n 1 -a 127.0.0.1') Do (
Set /A Cnt+=1
Set a[!Cnt:~-2!]=%%A
)
Set a[
Output
a[01]=Ping wird ausgeführt für Matar [127.0.0.1] mit 32 Bytes Daten:
a[02]=Antwort von 127.0.0.1: Bytes=32 Zeit<1ms TTL=128
a[03]=Ping-Statistik für 127.0.0.1:
a[04]= Pakete: Gesendet = 1, Empfangen = 1, Verloren = 0
a[05]= (0% Verlust),
a[06]=Ca. Zeitangaben in Millisek.:
a[07]= Minimum = 0ms, Maximum = 0ms, Mittelwert = 0ms
Actually I know this,but the expression is very complex,I just use the
echo **
to simulate my question. – yode – 2016-12-11T20:50:46.363I mean you cannot do the first step
set love=mycommand
:) – yode – 2016-12-11T20:55:08.443Not that easy as in Linux/Unix. Cmd.exe doesn't handle multiline vars. You have to use
for /f
to parse the output of one command and process it. – LotPings – 2016-12-11T21:02:02.627Oh,Would you mind to give a example for that?I'm a newbie for cmd.exe.And How about my second question?Can we use "|" to assign a variable? – yode – 2016-12-11T21:07:12.340
@yode I did append a sample, If asked for I'll explain in detail. – LotPings – 2016-12-11T21:28:36.317
Sorry,I'm in bed,I will response you when I wake in next morning. :) – yode – 2016-12-11T21:39:03.100
Acutuall I want assign all output to variable
a
not just that hostname – yode – 2016-12-12T07:13:23.730Cmd.exe CAN'T HANDLE MULTILINE VARS. I showed a way to circumvent that. If you insist on being that var a, I change the second batch, but in everycase you'll have the numbers attached. Well I think both of your questions are answered as best is possible. Decent hint: someone-answers
– LotPings – 2016-12-13T01:54:47.420