Forum > General

How to pipe process using TProcess

(1/3) > >>

arbelest:
Is Anybody know how to pipe prosess using Tprocess ?
For example, how to do this command :
   cat /etc/passwd | cut -d ":" -f 1

and also capture that output.
Now i solve this problem using shell, but this solution make my project less compatible with windows.
I have try using TProcess and apply that command, but i think TProcess ignore pipe, just execute cat /etc/passwd

Any suggestion here ?

Leledumbo:
TProcess is NOT a shell, it doesn't handle redirection via pipe operator, nor <, >, 2> etc. The best way is to call sh (or whatever your shell is) and pipe those string to the process (use the Input property) then read the output (using Output property). This sure is OS, actually shell, specific. You may want to put the platform dependent codes in conditional compilation blocks.

JuhaManninen:

--- Quote from: arbelest on July 01, 2010, 07:04:29 pm ---Any suggestion here ?

--- End quote ---

I would read the file like:

--- Code: ---StringList.LoadFromFile('/etc/passwd');

--- End code ---
and then parse it with pascal code.

Juha

theo:

--- Quote from: JuhaManninen on July 01, 2010, 11:42:59 pm ---I would read the file like:

--- End quote ---

Yes, it's easy. Look:


--- Code: ---Var sl: TStringList;
  dpos,i: integer;
Begin
  sl := TStringList.Create;
  Try
    sl.LoadFromFile('/etc/passwd');
    For i:=0 To sl.Count-1 Do
      Begin
        dpos := Pos(':',sl[i]);
        If dpos>0 Then sl[i] := Copy(sl[i],1,dpos-1);
      End;
    Memo1.text := sl.text; //Do something with result
  Finally
    sl.free;
  End;
End;

--- End code ---

arbelest:

--- Quote from: JuhaManninen on July 01, 2010, 11:42:59 pm ---I would read the file like:

--- Code: ---StringList.LoadFromFile('/etc/passwd');

--- End code ---
and then parse it with pascal code.

Juha


--- End quote ---

cat /etc/passwd is just an example.
In my project, i have to list available printer, so in shell i can write like this:
    lpstat -a | cut -d " " -f 1


--- Quote from: Leledumbo on July 01, 2010, 10:58:36 pm ---TProcess is NOT a shell, ..

--- End quote ---

Hmm, so i can't use pipe in TProcess. May be i'll try like this.
1. Create Tprocess an apply lpstat -a as it's command
2. Capture Tprocess output, an then parse like theo's code



Navigation

[0] Message Index

[#] Next page

Go to full version