List files opened

delphi package - easy access to kernel objects etc.
Post Reply
drphobos
Posts: 4
Joined: Fri Mar 23, 2007 10:36 am

List files opened

Post by drphobos »

Hi,

How I can create one list of the files opened on one directory/drive?

Thanks
drphobos
Posts: 4
Joined: Fri Mar 23, 2007 10:36 am

Post by drphobos »

I try this but the procedure is very slow.

Code: Select all

  procedure TForm3.Button1Click(Sender: TObject);
  var i1, i2: Integer;
  begin
    memo1.Clear;

    with Processes do
      for i1 := 0 to ItemCount - 1 do
        for i2 := 0 to Items[i1].Handles.ItemCount - 1 do
          begin
            Application.ProcessMessages;

            if not Items[i1].Handles.Items[i2].IsValid then Continue;
            if not (Items[i1].Handles.Items[i2].ObjType in [otFile, otFileMapping]) then Continue;
            if Items[i1].Handles.Items[i2].KernelObj.ObjName = '' then Continue;

            memo1.Lines.Add (Items[i1].Handles.Items[i2].KernelObj.ObjName);
          end;
  end;
:cry: :cry:
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

You can speed that up a bit by using "with" as often as possible. Methods like "IProcess.Handles" may otherwise redo the enumeration everytime you call it.

Code: Select all

procedure TForm3.Button1Click(Sender: TObject);
  var i1, i2: Integer;
      strVar: string;
  begin
    memo1.Clear;

    with Processes do
      for i1 := 0 to ItemCount - 1 do
        with Items[i1].Handles do
          for i2 := 0 to ItemCount - 1 do
            with Items[i2] do
            begin
              Application.ProcessMessages;

              if not IsValid then Continue;
              if not (ObjType in [otFile, otFileMapping]) then Continue;
              strVar := KernelObj.ObjName;
              if strVar = '' then Continue;

              memo1.Lines.Add (strVar);
            end;
    end;
See what I mean?
Post Reply