Recent

Author Topic: Problems in console application  (Read 3252 times)

emersonldc

  • Newbie
  • Posts: 3
Problems in console application
« on: March 22, 2018, 08:49:25 pm »
I need to create a console application that runs a command and get the return from command and save it to a variable. Can someone help me?
Below is the code I have made so far.

procedure TSDKMonitor.DoRun;
Var
  cmd: TProcess;
  Result: string;
begin
  try
    cmd := Tprocess.create(self);
    cmd.commandLine := 'ls -la';
    cmd.Execute;
  Finally
    cmd.free;
  end;
end;

balazsszekely

  • Guest
Re: Problems in console application
« Reply #1 on: March 22, 2018, 09:31:32 pm »

emersonldc

  • Newbie
  • Posts: 3
Re: Problems in console application
« Reply #2 on: March 22, 2018, 09:55:07 pm »
I even read this article, but I could not retrieve the information.

benohb

  • Full Member
  • ***
  • Posts: 213
Re: Problems in console application
« Reply #3 on: March 22, 2018, 11:36:43 pm »
Code: Pascal  [Select][+][-]
  1. procedure TSDKMonitor.DoRun;
  2. Var
  3.   cmd: TProcess;
  4.   Result: string;
  5.   AStringList: TStringList;
  6.  
  7.  
  8. begin
  9.   try
  10.     cmd := Tprocess.create(self);
  11.     cmd.commandLine := 'ls -la';
  12.     cmd.Options := cmd.Options + [poWaitOnExit, poUsePipes];
  13.     cmd.Execute;
  14.     AStringList := TStringList.Create;
  15.     AStringList.LoadFromStream(cmd.Output);
  16.   Finally
  17.     cmd.free;
  18.   end;
  19.  
  20.  
  21.   writeln(AStringList[0]); // first file/ first variable
  22.   writeln(AStringList[1]); //
  23.   AStringList.Free;
  24. end;

emersonldc

  • Newbie
  • Posts: 3
Re: Problems in console application
« Reply #4 on: March 23, 2018, 01:38:50 pm »
Perfect, it worked the way I needed it.
Thanks a lot for the help!

 

TinyPortal © 2005-2018