Solved

Subdirectory of \\computername

Posted on 2000-05-05
4
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

737 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