I need to open the contents of these files in the ListBox (a folder with many *.txt files). I would like to know a way to load all text files that are in that folder into a ListBox. I need to open all .txt files in listbox with a just click or procedure.
Asked
Active
Viewed 597 times
-2
-
1Emumerate the files, open each one, read its content, add to ListBox. Which step is confusing you exactly? Please be more specific. – Remy Lebeau Nov 22 '18 at 02:09
-
A listbox consists of items, not exactly suitable to display text files. – Sertac Akyuz Nov 22 '18 at 02:57
-
It is not necessary to put **help** in your title. It's clear you need help or you wouldn't be posting here. It's also not necessary to repeat the tag information in the title - the tagging system works very well here and doesn't need extra help. It would be of great benefit to you in terms of your experiences here if you spend some time taking the [tour] and reading the [help] pages, especially [ask], before you start posting here. – Ken White Nov 22 '18 at 04:37
-
okay sorry Thanks for the tips, I'll do it. – William Miguel Nov 22 '18 at 12:41
1 Answers
1
You can do something like this:
var
path: string;
SR: TSearchRec;
tempFile: TextFile;
line: string;
begin
path:= 'C:\ insert path to your folder\';
if FindFirst(path + '*.txt', faAnyFile, SR) = 0 then
begin
repeat
if (SR.Attr <> faDirectory) then
begin
AssignFile(tempFile, path + SR.Name);
Reset(tempFile);
while not Eof(tempFile) do
begin
Readln(tempFile, line);
ListBox1.Items.Add(line);
end;
end;
until FindNext(SR) <> 0;
FindClose(SR);
end;
end;
Put it on the button click or wrap it into the procedure.

Mr. Nice
- 357
- 2
- 16
-
Mr. Nice yeah, exactly what I needed! Thank you very much issue solved – William Miguel Nov 22 '18 at 12:34
-
@WilliamMiguel you are welcome. If solved the problem you could accept the answer. – Mr. Nice Nov 22 '18 at 12:49
-
Mr. Nice hey, I have a question.. i having some problems. I've been seeing a message lately "cannot create file ('c:\file.txt') the process cannot acess the file cuz it is being used by another process" how do I free memory of these .txt files? – William Miguel Feb 02 '19 at 00:45
-
why would you create file that already exists? you should just load file and not create it. https://stackoverflow.com/a/7830730/5789624 – Mr. Nice Feb 04 '19 at 06:55