How can I return only the value field from a command in batch? For example:
ipconfig /all | findstr "Host Name"
Will return
Host Name . . . . . . . . . . . . : YOUR_HOSTNAME
But I only want the value. In this case, YOUR_HOSTNAME.
Also, if there are more than one line, it should get the first match from the top.
for /f "tokens=1*delims=:" %b in ( 'ipconfig /all ^| findstr /c:"Host Name"') do set "hostname=%c"
set "hostname=%hostname:~1%"
from the prompt. Change %b
and %c
to %%b
and %%c
to use within a batch file.
The caret escapes the pipe, telling cmd
that the pipe is part of the command to be executed, not of the for
.
The /c:"Host Name"
makes findstr
find the precise string Host Name
. As you have it, findstr
would find either Host
or Name
, which would deliver a hit on
Ethernet adapter VirtualBox Host-Only Network:
Description . . . . . . . . . . . : VirtualBox Host-Only Ethernet Adapter
for instance.
To force the first match, append &goto label
to the for
command line and insert :label
between the two lines in a batch file.