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

Author Topic: Way to set a string to a command output  (Read 4109 times)

0 Members and 1 Guest are viewing this topic.

Z-Funk

  • Guest
Way to set a string to a command output
« on: October 20, 2007, 03:39:45 PM »
I'm using Windos Xp professional corporate edition.  i would like to know if there is a way to set a string to the output of a given command.

For example, the command "net config rdr" will give the following output:

Quote
Computer name                        \\C-8FCE127AFE9A2
Full Computer name                   c-8fce127afe9a21
User name                            Sauron

Workstation active on
        NetbiosSmb (000000000000)
        NetBT_Tcpip_{2FF4CC67-D07D-4FEB-85FD-860B1254C849} (00C0A8C16B59)
        NetBT_Tcpip_{8F676250-5DA4-424D-924E-1300965208B9} (00508DEB7F3E)
        NetBT_Tcpip_{45CF33C4-A270-4E9F-945F-C9FB6E1B0875} (005056C00001)
        NetBT_Tcpip_{26B53747-C608-45FC-8FBE-3C4A845AB750} (005056C00008)

Software version                     Windows 2002

Workstation domain                   RADEON
Workstation Domain DNS Name          (null)
Logon domain                         C-8FCE127AFE9A2

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

I would like to be able to set a string to the addresses under "Workstations active on:".  One of these:

Quote
NetbiosSmb (000000000000)
        NetBT_Tcpip_{2FF4CC67-D07D-4FEB-85FD-860B1254C849} (00C0A8C16B59)
        NetBT_Tcpip_{8F676250-5DA4-424D-924E-1300965208B9} (00508DEB7F3E)
        NetBT_Tcpip_{45CF33C4-A270-4E9F-945F-C9FB6E1B0875} (005056C00001)
        NetBT_Tcpip_{26B53747-C608-45FC-8FBE-3C4A845AB750} (005056C00008)

Is there a way to do this in a standard dos batch file on windows xp pro?

Thanks in advance.


contrex

  • Guest
Re: Way to set a string to a command output
« Reply #1 on: October 20, 2007, 05:30:21 PM »
I'm not helping anyone called Sauron!!

Seriously, I'll post something tomorrow.

Which workstation did you want? Are there always 4?


Z-Funk

  • Guest
Re: Way to set a string to a command output
« Reply #2 on: October 20, 2007, 05:53:48 PM »
I'm not helping anyone called Sauron!!

Seriously, I'll post something tomorrow.

Which workstation did you want? Are there always 4?

Ultimately, I wanted to let the user select from any one of the workstations.  There will not always be 4, it depends on how many network adapters will be on the machine.


contrex

  • Guest
Re: Way to set a string to a command output
« Reply #3 on: October 21, 2007, 05:54:05 AM »
This should illustrate the ideas needed:

Quote


@echo off
setlocal enabledelayedexpansion

REM using your command output in %temp%\fout.txt
REM use this in real situation
REM net config rdr > %temp%\fout.txt

echo.
echo Workstations found:
echo.

REM count 'em and list 'em

set /a Count=0
for /F "delims==" %%L in ('type %temp%\fout.txt ^| find " NetBT_Tcpip"') do (
   set /a Count += 1
   echo !Count! %%L
   )

echo.

REM get user to choose one

set /p chosennumber=Choose a number between 1 and %Count% :

REM get the string and put it in a variable

set /a Count=0
for /F "delims==" %%L in ('type fout.txt ^| find " NetBT_Tcpip"') do (
   set /a Count += 1
   if "!Count!"=="%chosennumber%" set chosenadaptor=%%L
   )

REM here it is

echo.
echo Adaptor Chosen: %chosennumber% %chosenadaptor%
echo.


Saved as ch-adapt.bat

Result of run:

Quote

S:\Test>ch-adapt

Workstations found:

1         NetBT_Tcpip_{2FF4CC67-D07D-4FEB-85FD-860B1254C849} (00C0A8C16B59)
2         NetBT_Tcpip_{8F676250-5DA4-424D-924E-1300965208B9} (00508DEB7F3E)
3         NetBT_Tcpip_{45CF33C4-A270-4E9F-945F-C9FB6E1B0875} (005056C00001)
4         NetBT_Tcpip_{26B53747-C608-45FC-8FBE-3C4A845AB750} (005056C00008)

Choose a number between 1 and 4 :3

Adaptor Chosen: 3         NetBT_Tcpip_{45CF33C4-A270-4E9F-945F-C9FB6E1B0875} (005056C00001)

S:\Test>


You still have leading spaces in the string, dunno if that will be a problem? Is the whole string desired, or part of it?




Z-Funk

  • Guest
Re: Way to set a string to a command output
« Reply #4 on: October 21, 2007, 06:10:28 AM »
Quote
You still have leading spaces in the string, dunno if that will be a problem? Is the whole string desired, or part of it?

First, thanks a lot. I thought it may involve making a separate file to first store the information.

I just need the adapter address from the string for example:

Quote
{2FF4CC67-D07D-4FEB-85FD-860B1254C849}

Agains, thanks for your help. 

I have a few questions since I'm new to batch programming.  It looks like essentially that your storing the output of the "net config rdr" in a filed called fout.txt in the temp folder.  Then you are sorting through that file looking for the identifying string "NetBT_Tcpip" storing them L, adding to a counter named "Count", then outputing the counter next to the the current string in L.

The second part looks like it is doing the same thing but that once the count variable equals the chosen number, then it stores the current string in L in chosen adapter.

Great, I will now try to see if I can isolate just the numeric values from the output.

« Last Edit: October 21, 2007, 06:34:27 AM by Z-Funk »

contrex

  • Guest
Re: Way to set a string to a command output
« Reply #5 on: October 21, 2007, 07:10:01 AM »
(1) You don't actually need a separate file. It's just that my PC is not on a LAN so i could not just use the output of the command directly. This is how you could do that (note single quotes)

for /f "delims==" %%L in ('net config rdr ^| find " NetBT_Tcpip"') do (
         commands
         )

(2) Do you need just the hex string or do you want the curly brackets ("braces" in America, I believe) as well?

If the string eg

        NetBT_Tcpip_{2FF4CC67-D07D-4FEB-85FD-860B1254C849} (00C0A8C16B59)

is always a fixed length, you can just select so many characters from the string (first char is position 0)

(with curly brackets) the 38 characters starting at position 20

set address=%chosenadaptor:~20,38%

(without curly brackets) the 36 characters starting at position 21

set address=%chosenadaptor:~21,36%

Or, parse out the chars in between the curly brackets (they will be the second token) with FOR

add these lines to the end of my above batch to see what I mean

Quote


REM parse out address

echo method (1)

echo address with curly brackets is     %chosenadaptor:~20,38%
echo address without curly brackets is  %chosenadaptor:~21,36%

echo method (2)

for /F "delims={} tokens=1,2" %%A in ("%chosenadaptor%") do set address=%%B

echo address with curly brackets is     {%address%}
echo address without curly brackets is  %address%


Quote

method (1)
address with curly brackets is     {45CF33C4-A270-4E9F-945F-C9FB6E1B0875}
address without curly brackets is  45CF33C4-A270-4E9F-945F-C9FB6E1B0875
method (2)
address with curly brackets is     {45CF33C4-A270-4E9F-945F-C9FB6E1B0875}
address without curly brackets is  45CF33C4-A270-4E9F-945F-C9FB6E1B0875


contrex

  • Guest
Re: Way to set a string to a command output
« Reply #6 on: October 21, 2007, 07:12:21 AM »
Quote
It looks like essentially that your storing the output of the "net config rdr" in a filed called fout.txt in the temp folder.  Then you are sorting through that file looking for the identifying string "NetBT_Tcpip" storing them L, adding to a counter named "Count", then outputing the counter next to the the current string in L.

The second part looks like it is doing the same thing but that once the count variable equals the chosen number, then it stores the current string in L in chosen adapter.

That is exactly right.


Z-Funk

  • Guest
Re: Way to set a string to a command output
« Reply #7 on: October 21, 2007, 07:36:05 AM »
You've been a great help, yea, I got the adapter addressed narrowed down using the numeric parameters ~20,38 (yes, I needed the curly brackets).

One more question, how do I concatenate strings, for example, once I get the adapter address in a string, I need it appended to another string not in the original output, something like:

Quote
Test.dll|{2FF4CC67-D07D-4FEB-85FD-860B1254C849}

Thanks again.  Ultimately, these strings are going to be fed as parameters to another command so the syntax has to be just right.

contrex

  • Guest
Re: Way to set a string to a command output
« Reply #8 on: October 21, 2007, 07:54:31 AM »
You've been a great help, yea, I got the adapter addressed narrowed down using the numeric parameters ~20,38 (yes, I needed the curly brackets).

One more question, how do I concatenate strings, for example, once I get the adapter address in a string, I need it appended to another string not in the original output, something like:

Quote
Test.dll|{2FF4CC67-D07D-4FEB-85FD-860B1254C849}

Thanks again.  Ultimately, these strings are going to be fed as parameters to another command so the syntax has to be just right.


Is that a pipe symbol | that I see after Test.dll?

To concatenate strings, just put them one after another including any spaces needed e.g.

Othercommand.bat Test.dll|%address%

Otherprogram.exe Test.dll | %chosenadaptor:~20,38%

can mix literal strings and variables

set pet1=cat
set pet2=dog

echo %pet1% and %pet2% and pony

set menagerie=%pet1% and %pet2% and pony