Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Batch findstr return only field value

How can I return only the value field from a command in batch?

ipconfig /all | findstr "Host Name"

Will return

Host Name . . . . . . . . . . . . : YOUR_HOSTNAME

But I only want the value, YOUR_HOSTNAME.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Also, if there are more than one line it would probably be best to get the first that matches.

>Solution :

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 findstrfind the precise stringHost Name. As you have it, findstrwould 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading