?
Solved

Subdirectory of \\computername

Posted on 2000-05-05
4
Medium Priority
?
286 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 400 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 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