PDA

Ver la Versión Completa : Problemas con TList<String>


Paulao
06-10-2011, 14:22:26
Señores, buenos dias. El que poasa es que mi aplicacion me lo da error de Access Violation quando yo intento llenar la lista con archivo. Vea abajo mi unit y mi codigo.

procedure TFileFinder.Start;
procedure PathFound(const Path: string);
var
Dir,texto,txt1,txt2: string;
Mask: string;
SR: TSearchRec;
erro,posicao,i:integer;
Lista:TList<String>;
nome,novo_nome,s: String;
begin
if Assigned(Lista) then
Lista := TList<string>.Create();

Dir := IncludeTrailingPathDelimiter(Path);
if not AceitarDirectory(Dir) then
Exit;

// Procura os arquivos
Mask := Dir + '*.*';
if Active and (FindFirst(Mask, faAnyFile - faDirectory, SR) = 0) then
try
//fNextDir := false;

repeat
inc(cont);
texto := '-'+StrZero(cont,4);
nome := Copy(SR.Name,1,Length(SR.Name)- 4);
//Vai copiar os arquivos do primeiro dir q encontra, q pode ser pdf,txt ou tif
if muda = 0 then
begin
Lista.AddRange([nome]);//Acá me lo da el error
txt1 := Dir + SR.Name;
Insert(texto,SR.Name,21);
txt2 := Dir + SR.Name;
end
else
begin
//passa a colocar o sequenciador de acordo com o nome do arquivo já existente na lista se é ou não igual
for i := 0 to Lista.Count - 1 do
begin
novo_nome := Copy(SR.Name,1,Length(SR.Name)- 4);
if novo_nome = Lista.Items[i] then
txt1 := Dir + novo_nome;
Insert(texto,novo_nome,21);
txt2 := Dir + novo_nome;
end;
end;
//Para renomear um arquivo, é necessário passar o path completo
RenameFile(txt1,txt2);
(*if(txt1 = txt2) then
ShowMessage('txt1 é igual a txt2');
if RenameFile(txt1,txt2) then
ShowMessage('Renomeou corretemente ' + txt1 + ' a ' + txt2)
else
begin
erro := GetLastError;
ShowMessage('Erro: ' + IntToStr(erro)+ ' ' +SysErrorMessage(erro)+'. Renomeando '+txt2+' a '+txt1);
end;*)
until (FindNext(SR) <> 0) or (not Active);// or fNextDir;

finally
FindClose(SR);
end;

// Percorre a arvore de diretórios
Mask := Dir + '*.*';
OnDir := Mask;
if Active and Recursive and (FindFirst(Mask, faDirectory, SR) = 0) then
try
repeat
if (SR.Name <> '.') and (SR.Name <> '..') then
begin
PathFound(Dir + SR.Name);
posicao := (Pos('PDF',SR.Name)) or (Pos('TIF',SR.Name))or (Pos('TXT',SR.Name));
if posicao > 0 then
cont := 0;
inc(muda);
end;
until (FindNext(SR) <> 0) or (not Active);
finally
FindClose(SR);
end;
end;

begin
PathFound(fStartPath);
Active := false;
end;

newtron
06-10-2011, 17:39:00
Hola.

Si no me equivoco el problema que estás teniendo es que no estás inicializando la lista, así que tendrías que cambiar esto:


if Assigned(Lista) then
Lista := TList<string>.Create();


por esto:


if not Assigned(Lista) then
Lista := TList<string>.Create();


y ya puestos, al final deberías liberarla con un free:


Lista.free;


Saludos

Ñuño Martínez
09-10-2011, 12:57:14
Y digo yo, ¿Por qué no usas TStringList?