1.

Solve : Search and copy?

Answer»

What I'm trying to find, is a way to search for a string of text that's outputted from a command, then COPY the next couple characters, and finally store them as a variable.

In case anyone needs it, the command I'm using is "net config rdr", and I want to use it to get the network adapter's GUID, however it outputs a lot of other things that I don't need, and I want just the GUID.

Thanks in advance!Code: [Select]@echo off
for /f "tokens=3 delims={}_" %%i in ('net config rdr ^| find /i "tcpip"') do echo GUID: %%i

The snippet is designed as a batch file. The GUID is in the variable %%i.

Thanks, I got my batch file to work perfectly.

However, if you can, do you mind explaining how your snippet works?

Thanks again!First run the initial command from the command line: net config rdr which produces:

Quote

Computer name \\LAPTOP
Full Computer name Laptop
User name *****

Workstation active on
NetbiosSmb (000000000000)
NetBT_Tcpip_{96C2160D-9E3B-4AE5-97CC-4B88AE6DB75F} (001150F4851B)

Software version Windows 2002

Workstation domain ********
Workstation Domain DNS Name (null)
Logon domain LAPTOP

COM Open Timeout (sec) 0
COM Send Count (BYTE) 16
COM Send Timeout (msec) 250
The command COMPLETED successfully.

Next use the find command to extract the line where the GUID resides: net config rdr | find /i "tcpip"

Quote
NetBT_Tcpip_{96C2160D-9E3B-4AE5-97CC-4B88AE6DB75F} (001150F4851B)

Next use the for command to parse the resulting output: for /f "tokens=3 delims={}_" %i in ('net config rdr | find /i "tcpip"') do echo GUID: %i

Quote
GUID: 96C2160D-9E3B-4AE5-97CC-4B88AE6DB75F

Finally, fix up the command for use in a batch file.

Code: [Select]@echo off
for /f "tokens=3 delims={}_" %%i in ('net config rdr ^| find /i "tcpip"') do echo GUID: %%i

The for command uses a combination of arbitrary tokens, delimiters, and a starting point of sequential addressable variables to do the parsing. For instance, this version of the for would be equally correct:

Code: [Select]@echo off
for /f "tokens=2 delims={}" %%x in ('net config rdr ^| find /i "netbt"') do echo GUID: %%x

Hmm, I'm not 100% sure of what the "for /f" and the "tokens=3 delims={}_" are for. Furthermore the "%i" variable is contained within the "for" command, and cannot be used outside.


Again, thanks for your THOROUGH REPLIES!


Discussion

No Comment Found