Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Search and copy  (Read 3358 times)

0 Members and 1 Guest are viewing this topic.

optix2000

  • Guest
Search and copy
« on: September 22, 2008, 09:23:19 PM »
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!

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Search and copy
« Reply #1 on: September 23, 2008, 03:06:06 AM »
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.

 8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

optix2000

  • Guest
Re: Search and copy
« Reply #2 on: September 23, 2008, 11:07:21 PM »
Thanks, I got my batch file to work perfectly.

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

Thanks again!

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Search and copy
« Reply #3 on: September 24, 2008, 05:06:23 AM »
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

 8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

optix2000

  • Guest
Re: Search and copy
« Reply #4 on: September 26, 2008, 11:55:41 PM »
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!
« Last Edit: September 27, 2008, 11:06:10 PM by optix2000 »