Ver Mensaje Individual
  #2  
Antiguo 21-11-2011
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Reputación: 38
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola.

Una forma podría ser:
Código Delphi [-]
...
uses StrUtils;

procedure TomarNombre(Nombre: string; var Nuevo: string);
var
  Num: Integer;
  Ext: string;
begin
  if FileExists(Nombre) then
  begin
    Ext := ExtractFileExt(Nombre);
    Nombre := LeftStr(Nombre, Length(Nombre) - Length(Ext));
    if Pos('(', Nombre) = 0 then
    begin
      Num := 1;
      Nombre := Nombre + '(' + IntToStr(Num) + ')' + Ext;
    end
    else
    begin
      Num := StrToInt(Copy(Nombre, Pos('(', Nombre)+1,
        PosEx(')',Nombre, Pos('(', Nombre))-Pos('(', Nombre)-1));
      Inc(Num);
      Nombre := Copy(Nombre,1,Pos('(', Nombre)-1)+'('+IntToStr(Num)+')' + Ext;
    end;
    if FileExists(Nombre) then
      TomarNombre(Nombre, Nombre);
  end;
  Nuevo:= Nombre;
end;

Llamada de ejemplo:
Código Delphi [-]
...
procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.Lines.LoadFromFile('C:\MIARCHIVO.TXT');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  s: string;
begin
  TomarNombre('C:\MIARCHIVO.TXT', s);
  Memo1.Lines.SaveToFile(s);
end;

Un saludo.
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita