delphi选择文件夹

 

 

//选择文件夹

function SelectFolderDialog(const Handle: integer;
  const Caption: string;
  const InitFolder: WideString;
  var SelectedFolder: string): boolean;
var
  BInfo: _browseinfo;
  Buffer: array[0..MAX_PATH] of
  Char;
  ID: IShellFolder;
  Eaten, Attribute: Cardinal;
  ItemID:
  PItemidlist;
begin
  Result := False;
  BInfo.HwndOwner := Handle;
  BInfo.lpfn := nil;
  BInfo.lpszTitle := Pchar(Caption);
  BInfo.ulFlags := BIF_RETURNONLYFSDIRS + BIF_NEWDIALOGSTYLE;
  SHGetDesktopFolder(ID);
  ID.ParseDisplayName(0, nil,
  PWideChar(InitFolder), Eaten, ItemID, Attribute);
  BInfo.pidlRoot := ItemID;
  GetMem(BInfo.pszDisplayName, MAX_PATH);
  try
    if SHGetPathFromIDList(SHBrowseForFolder(BInfo), Buffer) then
    begin
      SelectedFolder := Buffer;
      if Length(SelectedFolder) <> 3 then SelectedFolder := SelectedFolder + '\';
      result := True;
    end
    else
    begin
      SelectedFolder := '';
      Result := False;
    end;
  finally
    FreeMem(BInfo.pszDisplayName);
  end;
end;

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);


var
NewDir: string;


begin
if SelectFolderDialog(Handle, '选择', '', NewDir)
then
label1.Caption:=NewDir;
end;


end.

 

 


//搜索目录下所有文件


function MakeFileList(Path,FileExt:string):TStringList
;
var
sch:TSearchrec;
begin
Result:=TStringlist.Create;


if rightStr(trim(Path), 1) <> '\' then
    Path := trim(Path) +
'\'
else
    Path := trim(Path);


if not DirectoryExists(Path) then
begin
    Result.Clear;
   
exit;
end;


if FindFirst(Path + '*', faAnyfile, sch) = 0 then
begin
   
repeat
       Application.ProcessMessages;
       if ((sch.Name = '.') or
(sch.Name = '..')) then Continue;
       if DirectoryExists(Path+sch.Name)
then   // 这个地方加上一个判断,可以区别子文件夹河当前文件夹的操作
       begin
        
Result.AddStrings(MakeFileList(Path+sch.Name,FileExt));
       end
      
else
       begin
         if (UpperCase(extractfileext(Path+sch.Name)) =
UpperCase(FileExt)) or (FileExt='.*') then
        
Result.Add(Path+sch.Name);
       end;
    until FindNext(sch) <>
0;
    SysUtils.FindClose(sch);
end;
end;

//

ListBox1.Items:= MakeFileList(Label1.Caption ,'.*');//后面是类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi是一种基于对象的编程语言,广泛用于Windows平台的软件开发。要实现Delphi文件夹的扫描,可以采用递归的方法来遍历目录树,检查每个文件夹是否为空。 首先,创建一个函数来判断一个文件夹是否为空。这个函数将接受一个文件夹的路径作为参数,并返回一个布尔值。在函数中,我们可以使用TDirectory类来获取文件夹中的所有文件和子文件夹的信息。如果文件夹中没有文件和子文件夹,那么它就是一个空文件夹,函数将返回True;否则返回False。 接下来,创建另一个函数来扫描目标文件夹及其子文件夹。这个函数同样接受文件夹路径作为参数,并使用递归的方式遍历文件夹树。对于每个遍历到的文件夹,调用判断空文件夹的函数进行判断。如果发现一个空文件夹,可以将其路径记录下来或者执行其他自定义操作。 最后,在主程序中,调用扫描函数并传入要扫描的文件夹路径。可以使用界面元素(如按钮、输入框)来接收用户输入的文件夹路径。程序会递归地扫描该文件夹及其子文件夹,并输出或执行其他操作。 值得注意的是,为了避免无限递归或不必要的扫描,可以在递归函数中添加条件来限制文件夹的深度或者排除一些特定的文件夹。 使用Delphi来实现空文件夹扫描可以有效方便地检查目标文件夹中的空文件夹,提高文件夹管理的效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值