Solved

Subdirectory of \\computername

Posted on 2000-05-05
4
281 Views
Last Modified: 2010-04-04
I need some _code_ to get the subdirectories of \\computername in say a TStringList. Its easy to get the subdirectories/files from say \\computername\c-drive however findfirst doesnt work with \\computername.
0
Comment
Question by:Portal
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 100 total points
ID: 2782897
This works for Delphi 4 and 5:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ShellAPI;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses ShellFolders;

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var sf: TShellFolders;
begin
  sf := TShellFolders.Create('\\Babylon4');
  ListBox1.Clear;
  ListBox1.Items.AddStrings(sf.FileList);
  sf.Free;
end;

end.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2782898
unit ShellFolders;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ShlObj, ShellApi, ActiveX, ComCtrls;

type
  TShellFolders = class(TObject)
  private
    FShellMalloc: IMalloc;
    RootFolder: IShellFolder;
    function GetDisplayName(ShellFolder: IShellFolder;
                            PIDL: PItemIDList;
                            Flags: Cardinal): string;
    function GetSFFlags: DWORD;
    procedure Enumerate(parentfolder: IShellFolder; pidl: PItemIDList);
    procedure FreePIDL(PIDL: PItemIDList);
    function GetIDFromPath(const ShellFolder: IShellFolder;
                           const APath: string;
                           var ID: PItemIDList): boolean;
  public
    FileList: TStringList;
    constructor Create(Path: String);
    destructor Destroy; override;
  end;
                         
implementation

{ TShellFolders }

constructor TShellFolders.Create(Path: String);
var pidl: PItemIDList;
begin
  inherited Create;
  FileList := TStringList.Create;
  SHGetMalloc(FShellMalloc);
  SHGetDesktopFolder(RootFolder);
  if GetIDFromPath(RootFolder, Path, pidl) then
  begin
    Enumerate(RootFolder, pidl);
    FreePIDL(pidl);
  end;
end;

destructor TShellFolders.Destroy;
begin
  FileList.Free;
  inherited;
end;

procedure TShellFolders.Enumerate(parentfolder: IShellFolder; pidl: PItemIDList);
var tpidl: PItemIDList;
    folder: IShellFolder;
    EnumList: IEnumIDList;
    NumIDs: LongWord;
    Name: String;
begin
  parentfolder.BindToObject(pidl, nil, IID_IShellFolder, Pointer(folder));
  if folder.EnumObjects(Application.Handle,GetSFFlags,EnumList) = NO_ERROR then
  begin
    while EnumList.Next(1, tpidl, NumIDs) = NO_ERROR do
    begin
      Name := GetDisplayName(folder, tpidl, SHGDN_INFOLDER);
      FileList.Add(Name);
      FreePIDL(tpidl);
    end;
  end;
end;

function TShellFolders.GetSFFlags: DWORD;
begin
  Result:= SHCONTF_FOLDERS or SHCONTF_NONFOLDERS or SHCONTF_INCLUDEHIDDEN;
end;

function TShellFolders.GetDisplayName(ShellFolder: IShellFolder;
                               PIDL: PItemIDList;
                               Flags: Cardinal): string;
var StrRet: TStrRet;
    P: PChar;
begin
  Result := '';
  ShellFolder.GetDisplayNameOf(PIDL, Flags, StrRet);
  case StrRet.uType of
    STRRET_CSTR:
      SetString(Result, StrRet.cStr, lStrLen(StrRet.cStr));
    STRRET_OFFSET:
      begin
        P := @PIDL.mkid.abID[StrRet.uOffset - SizeOf(PIDL.mkid.cb)];
        SetString(Result, P, PIDL.mkid.cb - StrRet.uOffset);
      end;
    STRRET_WSTR:
      Result := StrRet.pOleStr;
  end;
end;

function TShellFolders.GetIDFromPath(const ShellFolder: IShellFolder;
                                  const APath: string;
                                  var ID: PItemIDList): boolean;
var
  OLEStr: array[0..MAX_PATH] of TOLEChar;
  Eaten: ULONG;
  Attr: ULONG;
begin
  if ShellFolder.ParseDisplayName(Application.Handle, NIL,
       StringToWideChar(APath, OLEStr, MAX_PATH),
       Eaten, ID, Attr) = NOERROR then
    Result:=true
  else
    Result:=false;
end;

procedure TShellFolders.FreePIDL(PIDL: PItemIDList);
begin
  if Assigned(PIDL) then FShellMalloc.Free(PIDL);
end;

end.
0
 
LVL 1

Expert Comment

by:l8knight
ID: 2783574
Epsylon: Nice Code.

But it only worked for me when I added a backslash to the path.

e.g. '\\Babylon4' should be '\\Babylon4\'

l8knight
0
 

Author Comment

by:Portal
ID: 2783705
Epsylon you are a god - fantastic answer and it also works in delphi 3 (with a change from longword to integer).
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

939 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now