Parsi Coders
تغییر نام فایل و پوشه در دلفی - نسخه قابل چاپ

+- Parsi Coders (http://parsicoders.com)
+-- انجمن: Software Development Programming (http://parsicoders.com/forumdisplay.php?fid=37)
+--- انجمن: Pascal/Delphi (http://parsicoders.com/forumdisplay.php?fid=45)
+---- انجمن: Delphi (http://parsicoders.com/forumdisplay.php?fid=69)
+---- موضوع: تغییر نام فایل و پوشه در دلفی (/showthread.php?tid=517)



تغییر نام فایل و پوشه در دلفی - Amin_Mansouri - 07-10-2011

کد:
// 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