Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How command results into stringlist.

Posted on 2009-02-09
5
Medium Priority
?
825 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
0
Comment
Question by:lloydie-t
  • 3
  • 2
5 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 23598327
what about using the > file
and then opening the file using a stringlist.loadfromfile

i suspect indeed you will need to use ShellExecute
0
 

Author Comment

by:lloydie-t
ID: 23598613
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.
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 23598819
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 23598925
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
0
 

Accepted Solution

by:
lloydie-t earned 0 total points
ID: 23599987
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

0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question