Solved

list files (dos like dir call)

Posted on 1997-07-20
2
162 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
ID: 1339363
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
ID: 1339364
Perfect!!!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Form ownership 4 90
How to debug For loops? 3 49
How to build JSON File in Delphi 6 3 34
Drag & Drop... Data from one grid to another 2 16
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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