We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How command results into stringlist.

lloydie-t
lloydie-t asked
on
Medium Priority
863 Views
Last Modified: 2013-11-11
I want to be able to read the results of a windows command into a stringlist. The following command:
getmac /v /fo csv | findstr Local
 
produces following results:
"Local Area Connection","Realtek RTL8102e Family PCI-E Fast Ethernet NIC (NDIS 6.0)","00-21-70-81-2C-96","Media Disconnected"
"Local Area Connection 2","Bluetooth Personal Area Network","00-21-86-A6-6E-27","Media Disconnected"

I suspect that I will have to use shellexecute. Any advice welcome
Comment
Watch Question

Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
what about using the > file
and then opening the file using a stringlist.loadfromfile

i suspect indeed you will need to use ShellExecute

Author

Commented:
I don't seem to be able to execute 'getmac /v /fo csv | findstr Local > macaddr.dat' in tpc. Also I have just found out the shellexecute will run asyncronously from the app so it may be that the outputed file will not be available to read.
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
i'm not that good at command line parsing myself yet,
i'm still in the process of learning it all
i use  this reference
http://technet.microsoft.com/nl-be/library/bb491071(en-us).aspx
I managed to work the solution out for myself. Because I am using freepascal the best option seemed to be Tprocess, which is supported in windows as well as linux.
Details here http://wiki.lazarus.freepascal.org/Executing_External_Programs
 function TGetHDser.GetMacAddr: String;
 var
 AProcess: TProcess;
 AStringList: TStringList;
 Found: Integer;
 i: integer;
begin
     AProcess := TProcess.Create(nil);
     AStringList := TStringList.Create;
     AStringList := TStringList.Create;
     AProcess.CommandLine := 'C:\Windows\System32\getmac.exe /v /fo csv';
     AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
     AProcess.Execute;
     AStringList.LoadFromStream(AProcess.Output);
   i := 0;
   while i < AStringList.Count do
   begin
   Found :=AnsiPos('Local', AStringList[i]);
      if Found = 0 then
        begin
        AStringList.Delete(i);
        continue;
        end;
      inc(i);
  end;
     AStringList.SaveToFile('macaddr2.txt'); //just for testing
     AStringList.Free;
     AProcess.Free;
end;

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.