Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 06-07-2011
Paulao Paulao is offline
Miembro
 
Registrado: sep 2003
Ubicación: Rua D 31 Casa 1 - Inhoaíba - Rio de Janeiro - RJ - Brasil
Posts: 637
Poder: 21
Paulao Va por buen camino
StringReplace no funciona

Mira, hizo una function para hacer un replace en algunos registros. Que pasa es que quando pongo un Break Point, si funciona, pero quando termina todo el proceso y me voy a la carpeta para ver los archivos, no funcionó, o sea, queda del la mismo forma como estava antes. Abajo mis codigos.

Código Delphi [-]
interface
 
uses Generics.Collections;
 
type
  TFileFoundEvent = procedure(Sender: TObject; const FileName: string) of object;
  TDirectoryFoundEvent = procedure(Sender: TObject; const FileName: string; var Aceitar: Boolean) of object;
 
  TFileFinder = class
  private
    fRecursive: Boolean;
    fOnFile: TFileFoundEvent;
    fFileMask: string;
    fOnDirectory: TDirectoryFoundEvent;
    fStartPath: string;
    fActive: Boolean;
    fNextDir: Boolean;
    fOnDir: string;
    fLista: TList<String>;
    fNomeArq: String;
  private
    procedure SetActive(const Value: Boolean);
    procedure SetStartPath(const Value: string);
  private
    function AceitarDirectory(const FileName: string): Boolean;
    procedure AceitarFile(const FileName: string);
    procedure Start;
    function TrocaNome(const texto: string):string;
  public
    constructor Create;
    procedure NextDirectory;
    property Active: Boolean read fActive write SetActive;
    property StartPath: string read fStartPath write SetStartPath;
    property Recursive: Boolean read fRecursive write fRecursive;
    property FileMask: string read fFileMask write fFileMask;
    property OnDirectory: TDirectoryFoundEvent read fOnDirectory
      write fOnDirectory;
    property OnFile: TFileFoundEvent read fOnFile write fOnFile;
    property OnDir: string read fOnDir write fOnDir;
    property ListaArquivo: TList<String> read fLista write fLista;
    property NomeArq: String read fNomeArq write fNomeArq;
  end;
 
implementation
 
uses SysUtils;
 
{ TFileFinder }
 
function TFileFinder.AceitarDirectory(const FileName: string): Boolean;
begin
  Result := Active;
  if Result and Assigned(OnDirectory) then
    OnDirectory(self, FileName, Result);
end;
 
procedure TFileFinder.AceitarFile(const FileName: string);
begin
  if Active and Assigned(OnFile) then
    OnFile(self, FileName);
end;
 
constructor TFileFinder.Create;
begin
  inherited Create;
  Recursive := true;
end;
 
procedure TFileFinder.NextDirectory;
begin
  fNextDir := true;
end;
 
procedure TFileFinder.SetActive(const Value: Boolean);
begin
  if fActive <> Value then
  begin
    fActive := Value and DirectoryExists(StartPath);
    if fActive then
      Start;
  end;
end;
 
procedure TFileFinder.SetStartPath(const Value: string);
begin
  fStartPath := IncludeTrailingPathDelimiter(Value);
end;
 
procedure TFileFinder.Start;
  procedure PathFound(const Path: string);
  var
    Dir: string;
    Mask: string;
    SR: TSearchRec;
    List: TList<String>;
    I,c: Integer;
  begin
    if Assigned(List) then
      List := TList<String>.Create();
 
    Dir := IncludeTrailingPathDelimiter(Path);
    if not AceitarDirectory(Dir) then
      Exit;
 
    // Procura os arquivos
    Mask := Dir + FileMask;
    if Active and (FindFirst(Mask, faAnyFile - faDirectory, SR) = 0) then
      try
        //fNextDir := false;
        repeat
          List.AddRange([SR.Name]);
          List.Sort;
          for I := 0 to List.Count - 1 do
          begin
            AceitarFile(Dir + List[i]{SR.Name});
          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);
            if ExtractFileName(SR.Name) <> '' then
              TrocaNome(SR.Name); //LLama la funcion para renombrar
          end;
        until (FindNext(SR) <> 0) or (not Active);
      finally
        FindClose(SR);
      end;
  end;
 
begin
  PathFound(fStartPath);
  Active := false;
end;
 
function TFileFinder.TrocaNome(const texto: string): string;
var
  s:string;
begin
  s := StringReplace(Texto,'NA','NAC',[rfReplaceAll]);
  Result := StringReplace(s,'Notícia','NOT',[rfReplaceAll]);
end;
 
end.

Última edición por Casimiro Notevi fecha: 06-07-2011 a las 20:14:26.
Responder Con Cita
  #2  
Antiguo 06-07-2011
Avatar de Caro
*Caro* Caro is offline
Moderadora
 
Registrado: jul 2004
Ubicación: Cochabamba, Bolivia
Posts: 2.544
Poder: 22
Caro Va por buen camino
Hola Paulao, si quieres renombrar el nombre de tus archivos, ademas de utilizar la función StringReplace debes utilizar la función RenameFile.

Código Delphi [-]
    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);
            if ExtractFileName(SR.Name) <> '' then
              begin
                Actual := Dir+SR.Name;
                Nuevo := Dir+TrocaNome(SR.Name);
                if not RenameFile(Actual, Nuevo) then
                 showmessage('el archivo ya existe, no se puede renombrar');                                      
              end;
          end;
        until (FindNext(SR) <> 0) or (not Active);
      finally
        FindClose(SR);
      end;

Saluditos
__________________
Disfruten cada minuto de su vida a lado de sus seres queridos como si fuese el ultimo, uno nunca sabe lo que puede pasar.
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
funciona bien en windows 7 64b pero en XP no funciona ASAPLTDA Varios 5 06-05-2011 16:24:50
No funciona PHP silviodp PHP 6 07-06-2008 21:51:29
StringReplace (posicionamiento cursor) jymy788 Varios 2 13-08-2007 11:48:27
Problema con StringReplace NeoTrooper Varios 2 31-07-2007 16:21:03
Error con StringReplace... FunBit Varios 8 15-11-2006 18:18:40


La franja horaria es GMT +2. Ahora son las 14:45:19.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi