hello i am trying to load a list of filenames from a folder into a Stringlist. I only want to add the names of files that end in .jpg When i run it now i get access violation errors. Here is what i got.
Selects directory.
procedure TMainForm.Load1Click(Sender: TObject);
var
DirName: string;
begin
mylist.Free;
myList := TList<TBitmap>.Create;
DirName := 'c:\';
if SelectDirectory(DirName,[sdAllowCreate, sdPerformCreate, sdPrompt],SELDIRHELP)
then;
LoadImages(DirName);
end;
Gets number of totalfiles in folder
function GetFilesCount(Dir : string; Mask : string) : integer;
var
Path : string;
begin
Result := 0;
for Path in TDirectory.GetFiles(Dir, Mask) do
inc(Result);
end;
Gets filenames from folder
function TMainForm.GetFilenames(Path: string ):TStrings;
var
Dest: TStrings;
SR: TSearchRec;
begin
Dest.create;
if FindFirst(Path+'*.*', faAnyFile, SR) = 0 then
repeat
Dest.Add(SR.Name);
until FindNext(SR) <> 0;
FindClose(SR);
Result := Dest;
Dest.Free;
end;
and the load iamges
procedure TMainForm.LoadImages(const Dir: string);
const
FIRST_IMAGE = 0;
var
iFile : Integer;
CurFileName: string;
FoundFile : boolean;
FileNameTemplate : string;
FileNames : Tstrings;
begin
FileNames.Create;
FileNameTemplate := IncludeTrailingPathDelimiter(Dir) + '*.jpg'; FileNames:=GetFileNames(FileNameTemplate);
try
ifile := 0;
repeat
CurFileName := FileNames.Names[ifile];
showmessage(Curfilename);
if FoundFile then
begin
end;
Inc(iFile);
end;
until not FoundFile;
end;
The problem is here (where you actually have two problems) – actually, you have the same problems twice in your code:
First,
TStringsis an abstract class. You can’t create an instance of it; it’s the basis for other more concrete classes likeTStringList. Create one of those concrete classes instead.Second, you have to create the
TStringListand store a reference to it.The better way to do it is to create your stringlist and pass it into the function or procedure, since you need the results when it returns. I’ll take a quick pass at one of them for you:
Call it like this: