Solved

list files (dos like dir call)

Posted on 1997-07-20
2
160 Views
Last Modified: 2010-04-04
how can I make a dos like dir call in delphi and store the info in a stringlist...ie given a string like 'c:\windows\files', how can I get the filenames from this directory and store them in a stringlist....please keep the answer simple (if possible). An example would be greatly appreciated.

Thanks in advance.
0
Comment
Question by:rafy
2 Comments
 
LVL 4

Accepted Solution

by:
erajoj earned 70 total points
Comment Utility
Do like this:

procedure DirToStrList(
  Dir: string; List: TStrings; FilesOnly: Boolean);
var
  SearchRec: TSearchRec;
begin
  List.Clear;                        // Just in case
  { Are there any files? }
  if FindFirst(Dir+'\*.*', faAnyFile, SearchRec)=0
  then repeat
    { Do we want dirs? }
    if (SearchRec.Attr<>faDirectory) or not FilesOnly
    then List.Add(SearchRec.Name); // Add to list
  until FindNext(SearchRec)<>0;    // Loop through all
  SysUtils.FindClose(SearchRec);   // Close to release memory
end;

Call like this:
  ( Root dir into ListBox1.Items, files only )
  DirToStrList('C:\', ListBox1.Items, True);

  ( "c:\windows\files" into Memo1.Lines, dirs allowed )
  DirToStrList('c:\windows\files', Memo1.Lines, False);

  ( "c:\windows\files" into new stringlist, files only )
  var
    Dir: TStringList;
  begin  
    Dir := TStringList.Create;
    DirToStrList('c:\windows\files', Dir, True);
    ...
    ...
    Dir.Free; // No need for this anymore!

---
Hope this helps!

/// John

0
 

Author Comment

by:rafy
Comment Utility
Perfect!!!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now