急急!!高手快来阿!!!向您求助!!!如何得到某一目录下面一级目录的目录名称列表?

时间:2021-01-13 14:33:14
如何得到某一目录下面一级目录的目录名称列表?比如我想得到跟目录C:\下一级所有目录列表,谢谢执教!

5 个解决方案

#1


怎么没反应啊!我正坐在公司的电脑旁急急的等啊。

#2


能帮的就帮一下吧,我已经上次把分数全送出去了,我在向同事要点,肯定给份的!!

#3


最简单的办法,放一个TDirectoryListBox和一个TListBox,然后。。。

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  ListBox1.Clear;
  for i := DirectoryListBox1.ItemIndex + 1 to DirectoryListBox1.Items.Count - 1 do
    ListBox1.Items.Add(DirectoryListBox1.Items[i]);
end;

#4


再给段代码:

procedure TForm1.Button2Click(Sender: TObject);
var
  Status: Integer;
  SearchRec: TSearchRec;
begin
  ListBox1.Clear;
  Status := FindFirst('C:\*.*', faDirectory, SearchRec);
  try
    while Status = 0 do
    begin
      if (SearchRec.Attr and faDirectory = faDirectory) then
      begin
        if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
        begin
          ListBox1.Items.Add(SearchRec.Name);
        end;
      end;
      Status := FindNext(SearchRec);
    end;
  finally
    FindClose(SearchRec);
  end;
end;

#5


给你一个例子吧,有几个函数对你有用,注释我没功夫写。如果好,多给点分啊,如下
unit MainFrm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, FileCtrl, Grids, Outline, DirOutln;

type
  TMainForm = class(TForm)
    dcbDrives: TDriveComboBox;
    edtFileMask: TEdit;
    lblFileMask: TLabel;
    btnSearchForFiles: TButton;
    lbFiles: TListBox;
    dolDirectories: TDirectoryOutline;
    procedure btnSearchForFilesClick(Sender: TObject);
    procedure dcbDrivesChange(Sender: TObject);
  private
    FFileName: String;
    function GetDirectoryName(Dir: String): String;
    procedure FindFiles(APath: String);
  end;

var
  MainForm: TMainForm;

implementation

{$R *.DFM}
function TMainForm.GetDirectoryName(Dir: String): String;
begin
  if Dir[Length(Dir)]<> '\' then
    Result := Dir+'\'
  else
    Result := Dir;
end;

procedure TMainForm.FindFiles(APath: String);
var
  FSearchRec,
  DSearchRec: TSearchRec;
  FindResult: integer;

  function IsDirNotation(ADirName: String): Boolean;
  begin
    Result := (ADirName = '.') or (ADirName = '..');
  end;

begin
  APath := GetDirectoryName(APath);

  FindResult := FindFirst(APath+FFileName,faAnyFile+faHidden+
                          faSysFile+faReadOnly,FSearchRec);
  try
    while FindResult = 0 do
    begin
      lbFiles.Items.Add(LowerCase(APath+FSearchRec.Name));
      FindResult := FindNext(FSearchRec);
    end;

    FindResult := FindFirst(APath+'*.*', faDirectory, DSearchRec);

    while FindResult = 0 do
    begin
      if ((DSearchRec.Attr and faDirectory) = faDirectory) and not
        IsDirNotation(DSearchRec.Name) then
        FindFiles(APath+DSearchRec.Name); // Recursion here
      FindResult := FindNext(DSearchRec);
    end;
  finally
    FindClose(FSearchRec);
  end;
end;

procedure TMainForm.btnSearchForFilesClick(Sender: TObject);
begin
  Screen.Cursor := crHourGlass;
  try
    lbFiles.Items.Clear;
    FFileName := edtFileMask.Text;
    FindFiles(dolDirectories.Directory);
  finally
    Screen.Cursor := crDefault;
  end;
end;

procedure TMainForm.dcbDrivesChange(Sender: TObject);
begin
  dolDirectories.Drive := dcbDrives.Drive;
end;



end.
-------------------------form-------------------
object MainForm: TMainForm
  Left = 218
  Top = 156
  Width = 552
  Height = 300
  Caption = '目录搜索示例'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  Position = poScreenCenter
  PixelsPerInch = 96
  TextHeight = 13
  object lblFileMask: TLabel
    Left = 12
    Top = 228
    Width = 45
    Height = 13
    Caption = 'File Mask'
  end
  object dcbDrives: TDriveComboBox
    Left = 12
    Top = 8
    Width = 169
    Height = 19
    TabOrder = 0
    OnChange = dcbDrivesChange
  end
  object edtFileMask: TEdit
    Left = 12
    Top = 244
    Width = 177
    Height = 21
    TabOrder = 1
    Text = '*.pas'
  end
  object btnSearchForFiles: TButton
    Left = 196
    Top = 240
    Width = 337
    Height = 25
    Caption = '搜索需要的文件'
    TabOrder = 2
    OnClick = btnSearchForFilesClick
  end
  object lbFiles: TListBox
    Left = 192
    Top = 8
    Width = 341
    Height = 213
    ItemHeight = 13
    TabOrder = 3
  end
  object dolDirectories: TDirectoryOutline
    Left = 12
    Top = 28
    Width = 169
    Height = 197
    ItemHeight = 13
    Options = [ooDrawFocusRect]
    PictureLeaf.Data = {
      16050000424D160500000000000036040000280000000E0000000E0000000100
      080000000000E000000000000000000000000001000000010000000000000000
      80000080000000808000800000008000800080800000C0C0C000C0DCC000F0CA
      A600F0FBFF00A4A0A000808080000000FF0000FF000000FFFF00FF000000FF00
      FF00FFFF0000FFFFFF0000000000000080000080000000808000800000008000
      800080800000C0C0C000C0DCC000F0CAA600F0FBFF00A4A0A000808080000000
      FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00000000000000
      80000080000000808000800000008000800080800000C0C0C000C0DCC000F0CA
      A600F0FBFF00A4A0A000808080000000FF0000FF000000FFFF00FF000000FF00
      FF00FFFF0000FFFFFF0000000000000080000080000000808000800000008000
      800080800000C0C0C000C0DCC000F0CAA600F0FBFF00A4A0A000808080000000
      FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00000000000000
      80000080000000808000800000008000800080800000C0C0C000C0DCC000F0CA
      A600F0FBFF00A4A0A000808080000000FF0000FF000000FFFF00FF000000FF00
      FF00FFFF0000FFFFFF0000000000000080000080000000808000800000008000
      800080800000C0C0C000C0DCC000F0CAA600F0FBFF00A4A0A000808080000000
      FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00000000000000
      80000080000000808000800000008000800080800000C0C0C000C0DCC000F0CA
      A600F0FBFF00A4A0A000808080000000FF0000FF000000FFFF00FF000000FF00
      FF00FFFF0000FFFFFF0000000000000080000080000000808000800000008000
      800080800000C0C0C000C0DCC000F0CAA600F0FBFF00A4A0A000808080000000
      FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00000000000000
      80000080000000808000800000008000800080800000C0C0C000C0DCC000F0CA
      A600F0FBFF00A4A0A000808080000000FF0000FF000000FFFF00FF000000FF00
      FF00FFFF0000FFFFFF0000000000000080000080000000808000800000008000
      800080800000C0C0C000C0DCC000F0CAA600F0FBFF00A4A0A000808080000000
      FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00000000000000
      80000080000000808000800000008000800080800000C0C0C000C0DCC000F0CA
      A600F0FBFF00A4A0A000808080000000FF0000FF000000FFFF00FF000000FF00
      FF00FFFF0000FFFFFF0000000000000080000080000000808000800000008000
      800080800000C0C0C000C0DCC000F0CAA600F0FBFF00A4A0A000808080000000
      FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00000000000000
      800000800000008080008000000080008000F0FBFF00A4A0A000808080000000
      FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00050505050505
      0505050505050505000005050505050505050505050505050000050505050505
      0505050505050505000005000000000000000000000505050000000F130F130F
      130F130F00050505000000130F130F130F130F13000505050000000F130F130F
      130F130F00050505000000130F130F130F130F13000505050000000F130F130F
      130F130F00050505000000130F130F130F130F13000505050000000000000000
      000000000505050500000500130F130F00050505050505050000050C00000000
      0C05050505050505000005050505050505050505050505050000}
    TabOrder = 4
    Data = {10}
  end
end

#1


怎么没反应啊!我正坐在公司的电脑旁急急的等啊。

#2


能帮的就帮一下吧,我已经上次把分数全送出去了,我在向同事要点,肯定给份的!!

#3


最简单的办法,放一个TDirectoryListBox和一个TListBox,然后。。。

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  ListBox1.Clear;
  for i := DirectoryListBox1.ItemIndex + 1 to DirectoryListBox1.Items.Count - 1 do
    ListBox1.Items.Add(DirectoryListBox1.Items[i]);
end;

#4


再给段代码:

procedure TForm1.Button2Click(Sender: TObject);
var
  Status: Integer;
  SearchRec: TSearchRec;
begin
  ListBox1.Clear;
  Status := FindFirst('C:\*.*', faDirectory, SearchRec);
  try
    while Status = 0 do
    begin
      if (SearchRec.Attr and faDirectory = faDirectory) then
      begin
        if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
        begin
          ListBox1.Items.Add(SearchRec.Name);
        end;
      end;
      Status := FindNext(SearchRec);
    end;
  finally
    FindClose(SearchRec);
  end;
end;

#5


给你一个例子吧,有几个函数对你有用,注释我没功夫写。如果好,多给点分啊,如下
unit MainFrm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, FileCtrl, Grids, Outline, DirOutln;

type
  TMainForm = class(TForm)
    dcbDrives: TDriveComboBox;
    edtFileMask: TEdit;
    lblFileMask: TLabel;
    btnSearchForFiles: TButton;
    lbFiles: TListBox;
    dolDirectories: TDirectoryOutline;
    procedure btnSearchForFilesClick(Sender: TObject);
    procedure dcbDrivesChange(Sender: TObject);
  private
    FFileName: String;
    function GetDirectoryName(Dir: String): String;
    procedure FindFiles(APath: String);
  end;

var
  MainForm: TMainForm;

implementation

{$R *.DFM}
function TMainForm.GetDirectoryName(Dir: String): String;
begin
  if Dir[Length(Dir)]<> '\' then
    Result := Dir+'\'
  else
    Result := Dir;
end;

procedure TMainForm.FindFiles(APath: String);
var
  FSearchRec,
  DSearchRec: TSearchRec;
  FindResult: integer;

  function IsDirNotation(ADirName: String): Boolean;
  begin
    Result := (ADirName = '.') or (ADirName = '..');
  end;

begin
  APath := GetDirectoryName(APath);

  FindResult := FindFirst(APath+FFileName,faAnyFile+faHidden+
                          faSysFile+faReadOnly,FSearchRec);
  try
    while FindResult = 0 do
    begin
      lbFiles.Items.Add(LowerCase(APath+FSearchRec.Name));
      FindResult := FindNext(FSearchRec);
    end;

    FindResult := FindFirst(APath+'*.*', faDirectory, DSearchRec);

    while FindResult = 0 do
    begin
      if ((DSearchRec.Attr and faDirectory) = faDirectory) and not
        IsDirNotation(DSearchRec.Name) then
        FindFiles(APath+DSearchRec.Name); // Recursion here
      FindResult := FindNext(DSearchRec);
    end;
  finally
    FindClose(FSearchRec);
  end;
end;

procedure TMainForm.btnSearchForFilesClick(Sender: TObject);
begin
  Screen.Cursor := crHourGlass;
  try
    lbFiles.Items.Clear;
    FFileName := edtFileMask.Text;
    FindFiles(dolDirectories.Directory);
  finally
    Screen.Cursor := crDefault;
  end;
end;

procedure TMainForm.dcbDrivesChange(Sender: TObject);
begin
  dolDirectories.Drive := dcbDrives.Drive;
end;



end.
-------------------------form-------------------
object MainForm: TMainForm
  Left = 218
  Top = 156
  Width = 552
  Height = 300
  Caption = '目录搜索示例'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  Position = poScreenCenter
  PixelsPerInch = 96
  TextHeight = 13
  object lblFileMask: TLabel
    Left = 12
    Top = 228
    Width = 45
    Height = 13
    Caption = 'File Mask'
  end
  object dcbDrives: TDriveComboBox
    Left = 12
    Top = 8
    Width = 169
    Height = 19
    TabOrder = 0
    OnChange = dcbDrivesChange
  end
  object edtFileMask: TEdit
    Left = 12
    Top = 244
    Width = 177
    Height = 21
    TabOrder = 1
    Text = '*.pas'
  end
  object btnSearchForFiles: TButton
    Left = 196
    Top = 240
    Width = 337
    Height = 25
    Caption = '搜索需要的文件'
    TabOrder = 2
    OnClick = btnSearchForFilesClick
  end
  object lbFiles: TListBox
    Left = 192
    Top = 8
    Width = 341
    Height = 213
    ItemHeight = 13
    TabOrder = 3
  end
  object dolDirectories: TDirectoryOutline
    Left = 12
    Top = 28
    Width = 169
    Height = 197
    ItemHeight = 13
    Options = [ooDrawFocusRect]
    PictureLeaf.Data = {
      16050000424D160500000000000036040000280000000E0000000E0000000100
      080000000000E000000000000000000000000001000000010000000000000000
      80000080000000808000800000008000800080800000C0C0C000C0DCC000F0CA
      A600F0FBFF00A4A0A000808080000000FF0000FF000000FFFF00FF000000FF00
      FF00FFFF0000FFFFFF0000000000000080000080000000808000800000008000
      800080800000C0C0C000C0DCC000F0CAA600F0FBFF00A4A0A000808080000000
      FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00000000000000
      80000080000000808000800000008000800080800000C0C0C000C0DCC000F0CA
      A600F0FBFF00A4A0A000808080000000FF0000FF000000FFFF00FF000000FF00
      FF00FFFF0000FFFFFF0000000000000080000080000000808000800000008000
      800080800000C0C0C000C0DCC000F0CAA600F0FBFF00A4A0A000808080000000
      FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00000000000000
      80000080000000808000800000008000800080800000C0C0C000C0DCC000F0CA
      A600F0FBFF00A4A0A000808080000000FF0000FF000000FFFF00FF000000FF00
      FF00FFFF0000FFFFFF0000000000000080000080000000808000800000008000
      800080800000C0C0C000C0DCC000F0CAA600F0FBFF00A4A0A000808080000000
      FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00000000000000
      80000080000000808000800000008000800080800000C0C0C000C0DCC000F0CA
      A600F0FBFF00A4A0A000808080000000FF0000FF000000FFFF00FF000000FF00
      FF00FFFF0000FFFFFF0000000000000080000080000000808000800000008000
      800080800000C0C0C000C0DCC000F0CAA600F0FBFF00A4A0A000808080000000
      FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00000000000000
      80000080000000808000800000008000800080800000C0C0C000C0DCC000F0CA
      A600F0FBFF00A4A0A000808080000000FF0000FF000000FFFF00FF000000FF00
      FF00FFFF0000FFFFFF0000000000000080000080000000808000800000008000
      800080800000C0C0C000C0DCC000F0CAA600F0FBFF00A4A0A000808080000000
      FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00000000000000
      80000080000000808000800000008000800080800000C0C0C000C0DCC000F0CA
      A600F0FBFF00A4A0A000808080000000FF0000FF000000FFFF00FF000000FF00
      FF00FFFF0000FFFFFF0000000000000080000080000000808000800000008000
      800080800000C0C0C000C0DCC000F0CAA600F0FBFF00A4A0A000808080000000
      FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00000000000000
      800000800000008080008000000080008000F0FBFF00A4A0A000808080000000
      FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00050505050505
      0505050505050505000005050505050505050505050505050000050505050505
      0505050505050505000005000000000000000000000505050000000F130F130F
      130F130F00050505000000130F130F130F130F13000505050000000F130F130F
      130F130F00050505000000130F130F130F130F13000505050000000F130F130F
      130F130F00050505000000130F130F130F130F13000505050000000000000000
      000000000505050500000500130F130F00050505050505050000050C00000000
      0C05050505050505000005050505050505050505050505050000}
    TabOrder = 4
    Data = {10}
  end
end