Parsi Coders

نسخه‌ی کامل: تغییر نام فایل و پوشه در دلفی
شما در حال مشاهده نسخه آرشیو هستید. برای مشاهده نسخه کامل کلیک کنید.
کد:
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.

unit Unit1;

interface

uses
  SysUtils,   // Unit containing the RenameFile command
  Forms, Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation
{$R *.dfm} // Include form definitions

procedure TForm1.FormCreate(Sender: TObject);
var
  oldName, newName : string;

begin
  // Try to rename the current Unit1.dcu to Uni1.old
  oldName := 'Unit1.dcu';
  newName := ChangeFileExt(oldName, '.old');
  if RenameFile(oldName, newName)
  then ShowMessage('Unit1.dcu renamed OK')
  else ShowMessage('Unit1.dcu rename failed with error : '+
                   IntToStr(GetLastError));

  // Let us try the same rename again
  if RenameFile(oldName, newName)
  then ShowMessage('Unit1.dcu renamed again OK')
  else ShowMessage('Unit1.dcu rename failed with error : '+
                   IntToStr(GetLastError));

  // Finally, let us rename the file back again
  if RenameFile(newName, oldName)
  then ShowMessage('Unit1.old renamed back OK')
  else ShowMessage('Unit1.old rename back failed with error : '+
                   IntToStr(GetLastError));
end;

end