Solved

Subdirectory of \\computername

Posted on 2000-05-05
4
280 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Epsylon you are a god - fantastic answer and it also works in delphi 3 (with a change from longword to integer).
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

10 Experts available now in Live!

Get 1:1 Help Now