PDA

Ver la Versión Completa : No consigo renombrar un archivo


Paulao
03-10-2011, 01:24:44
Mira, no estoy conseguindo renombrar un archivo con RenameFile en una busca con SearchRec. Abajo mi rutina que deberia renombrar mis archivos.
procedure TFileFinder.Start;
procedure PathFound(const Path: string);
var
Dir,texto,txt1,txt2,txtfinal,ext: string;
Mask: string;
SR: TSearchRec;
begin

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;
inc(cont);
texto := '-'+StrZero(cont,4);
repeat
txt1 := SR.Name;
Insert(texto,SR.Name,21);
txt2 := SR.Name;
RenameFile(txt1,txt2);
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
PathFound(Dir + SR.Name);
until (FindNext(SR) <> 0) or (not Active);
finally
FindClose(SR);
end;
end;

dec
03-10-2011, 02:46:05
Hola,

Lo primero que se me ocurre (no das información acerca del posible error, si es que lo hay) es que el archivo esté en uso, sin ir más lejos, por el propio objeto "TSearchRec". Igual podrías buscar los archivos primero, almacenar los cambios a realizar de alguna forma, y, después, renombrar los archivos. No sé. Es una idea. ;)

duilioisola
03-10-2011, 09:16:35
txt1 y txt2 puede que tengan el mismo nombre si Insert(...) no hace bien su trabajo.
Estás seguro de que txt1 y txt2 son diferentes?

txt1 := SR.Name;
Insert(texto,SR.Name,21);
txt2 := SR.Name;
if (txt1 = txt2) then
ShowMessage('txt1 y txt2 son iguales!');
RenameFile(txt1,txt2);

Paulao
04-10-2011, 01:49:04
No, no son iguales no. Quando inserto una string, SR.Name ya pasa a ser diferente, luego txt1 <> txt2. No da ningun mensaje de error. Simplesmente no cambia.

duilioisola
04-10-2011, 09:06:45
Prueba agregar esto, para saber cual es el error.
Si no lo puede renombrar te dará un código de error.

var
CodigoError : integer;
...
txt1 := SR.Name;
Insert(texto,SR.Name,21);
txt2 := SR.Name;
if (txt1 = txt2) then
ShowMessage('txt1 y txt2 son iguales!');
if RenameFile(txt1,txt2) then
ShowMessage('Se renombro correctamente ' + txt1 + ' a ' + txt2)
else
begin
CodigoError := GetLastError;
ShowMessage('Error ' + IntToStr(CodigoError) + ' ' + SysErrorMessage(CodigoError) + ' renombrando 'txt1 + ' a ' + txt2)
end;

Paulao
05-10-2011, 16:56:16
Ok, aun no tuvo tiempo para ejecutar esto.

Paulao
05-10-2011, 17:49:36
Bueno, estoy haciendo los testes ahora y me lo da el seguinte error:
Error 2. El sistema no puede encuentrar el archivo especificado. Renombrando....
Voy a cambiar la forma de renombrar para ver si es esto.

Paulao
05-10-2011, 20:19:34
Bueno, decobri el problema. El problema estava entre el teclado y la pantalla,ehehehe. Es que yo no estava pasando todo del archivo, solamente el nombre y deberia se el nombre, con su Path, o sea, Path + Nombre. Cambie y funciono.