Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8224665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:09:03+00:00 2026-06-07T15:09:03+00:00

I am working with Delphi 7 and I would like to list all the

  • 0

I am working with Delphi 7 and I would like to list all the files in a given directory in a string grid (one file per row and all in 1 column). I have searched for about an hour now and cannot find any examples on how to do this so any help you can provide would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:09:07+00:00Added an answer on June 7, 2026 at 3:09 pm

    This will fill a TStrings descendant (eg., TStringList, TMemo.Lihes, and so forth) with all of the files in a specified folder:

    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;
    

    Use it like this to populate your TStringGrid (Grid in the code below – I added code to auto-size the column based on the length of the longest filename):

    var
      SL: TStringList;
      i: Integer;
      MaxWidth, CurrWidth: Integer;
    const
      Padding = 10;
    begin
      SL := TStringList.Create;
      try
        if GetFiles('C:\Temp\', SL) then
        begin
          MaxWidth := Grid.ColWidths[0];
          for i := 0 to SL.Count - 1 do
          begin
            CurrWidth := Grid.Canvas.TextWidth(SL[i]);
            if CurrWidth > MaxWidth then
              MaxWidth := CurrWidth;
            // Populates first column in stringgrid.
            Grid.RowCount := Grid.RowCount + 1;
            Grid.Cells[0, Grid.RowCount - 1] := SL[i];
          end;
          Grid.ColWidths[0] := MaxWidth + Padding;
        end;
      finally
        SL.Free;
      end;
    end;
    

    Note that this code requires the path to include the trailing backslash after the folder name; you can modify it easily to automatically add it if needed, or to accept both a folder name and a file mask to include only certain files.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a component installer (only for Delphi XE2) and I would like
I would like to make a function IsWordPronounceable(SomeWord:String): boolean; english language i am working
I am working on an MDI application in Delphi. I would like to show
I would like to declare a record in Delphi that contains the same layout
I'm working in Delphi 2007. Net, where I can find an example of using
I followed these instructions while trying to get remote debugging working with Delphi 2007.
Working on a service application in Delphi 5 that is intended to run on
i am working on a custom component in Delphi -7 for which i have
Version used: Delphi 7. I'm working on a program that does a simple for
I have a simple Delphi program that I'm working on, in which I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.