Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用delphi在字符串网格中列出目录中的所有文件_Delphi_Tstringgrid_Filelist - Fatal编程技术网

使用delphi在字符串网格中列出目录中的所有文件

使用delphi在字符串网格中列出目录中的所有文件,delphi,tstringgrid,filelist,Delphi,Tstringgrid,Filelist,我正在使用Delphi7,我想在一个字符串网格中列出给定目录中的所有文件(每行一个文件,每列一个文件)。我已经搜索了大约一个小时了,但找不到任何关于如何做到这一点的例子,因此如果您能提供任何帮助,我将不胜感激 用于获取文件 只需在所需的行/列处插入字符串。根据需要增加“行数”: 用于获取文件 只需在所需的行/列处插入字符串。根据需要增加“行数”: 这将使用指定文件夹中的所有文件填充TStrings子体(例如TStringList,TMemo.Lihes,等等): functi

我正在使用Delphi7,我想在一个字符串网格中列出给定目录中的所有文件(每行一个文件,每列一个文件)。我已经搜索了大约一个小时了,但找不到任何关于如何做到这一点的例子,因此如果您能提供任何帮助,我将不胜感激

  • 用于获取文件

  • 只需在所需的行/列处插入字符串。根据需要增加“行数”:

  • 用于获取文件

  • 只需在所需的行/列处插入字符串。根据需要增加“行数”:


  • 这将使用指定文件夹中的所有文件填充
    TStrings
    子体(例如
    TStringList
    TMemo.Lihes
    ,等等):

    function  GetFiles(const StartDir: String; const List: TStrings): Boolean;
    var
      SRec: TSearchRec;
      Res: Integer;
    begin
      if not Assigned(List) then
      begin
        Result := False;
        Exit;
      end;
      Res := FindFirst(StartDir + '*.*', faAnyfile, SRec );
      if Res = 0 then
      try
        while res = 0 do
        begin
          if (SRec.Attr and faDirectory <> faDirectory) then
            // If you want filename only, remove "StartDir +" 
            // from next line
            List.Add( StartDir + SRec.Name );
          Res := FindNext(SRec);
        end;
      finally
        FindClose(SRec)
      end;
      Result := (List.Count > 0);
    end;
    

    请注意,此代码要求路径在文件夹名称后包含尾随的反斜杠;您可以轻松地修改它,以便在需要时自动添加它,或者接受文件夹名称和文件掩码以仅包含某些文件。

    这将使用指定文件夹中的所有文件填充
    TStrings
    子代(例如
    TStringList
    TMemo.Lihes
    ,等等):

    function  GetFiles(const StartDir: String; const List: TStrings): Boolean;
    var
      SRec: TSearchRec;
      Res: Integer;
    begin
      if not Assigned(List) then
      begin
        Result := False;
        Exit;
      end;
      Res := FindFirst(StartDir + '*.*', faAnyfile, SRec );
      if Res = 0 then
      try
        while res = 0 do
        begin
          if (SRec.Attr and faDirectory <> faDirectory) then
            // If you want filename only, remove "StartDir +" 
            // from next line
            List.Add( StartDir + SRec.Name );
          Res := FindNext(SRec);
        end;
      finally
        FindClose(SRec)
      end;
      Result := (List.Count > 0);
    end;
    

    请注意,此代码要求路径在文件夹名称后包含尾随的反斜杠;您可以轻松地修改它,以便在需要时自动添加它,或者接受文件夹名称和文件掩码以仅包含某些文件。

    这似乎列出了文件夹,但没有列出文件,或者我做了一些错误的操作。您错过了我的编辑。:-)将
    =faDirectory
    更改为
    faDirectory
    。这似乎列出了文件夹,但没有列出文件,或者我的操作不正确。您错过了我的编辑。:-)将
    =faDirectory
    更改为
    faDirectory