Home  |  Delphi .net Home  |  System.IO.DirectoryInfo  |  MoveTo Method
MoveTo  
Method  
Moves a Directory (folder) to a new location
DirectoryInfo Class
System.IO NameSpace
CF1.  Procedure MoveTo ( TargetDirectoryPath : String; ) ;
CF : Methods with this mark are Compact Framework Compatible
Description
This is a bit of a confusing method. You specify the new TargetDirectoryPath string as the location for moving the current Directory and its contents.
 
In basic usage, you give the name of the new Directory to be created. It must be different from the current Directory name, and must not exist.
 
However, if it does exist, then the current directory is moved with its current name to a subdirectory of this existing directory.
 
Important : when specifying a relative directory, the new directory is relative to the current directory (as given by System.IO.Directory.GetCurrentDirectory) and not the current object folder location! This is not so obvious to see at first!
Microsoft MSDN Links
System.IO
System.IO.DirectoryInfo
 
 
A simple example
// Full Unit code.
// -------------------------------------------------------------
// Create a new WinForm application, double click the form to
// create an OnLoad event, and then replace the WinForm unit
// with this text.
 
unit WinForm;
 
interface
 
uses
  System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data;
 
type
  TWinForm = class(System.Windows.Forms.Form)
  \{REGION 'Designer Managed Code'\} // Note that REGION and ENREGION should be prefixed by a dollar sign
  strict private
    ///
    /// Required designer variable.
    ///

    Components: System.ComponentModel.Container;
    ///
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    ///

    procedure InitializeComponent;
    procedure TWinForm_Load(sender: System.Object; e: System.EventArgs);
  {ENDREGION}
  strict protected
    ///
    /// Clean up any resources being used.
    ///

    procedure Dispose(Disposing: Boolean); override;
  private
    { Private Declarations }
  public
    constructor Create;
  end;
 
  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]
 
implementation
 
\{REGION 'Windows Form Designer generated code'\}
///
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
///

 
 
procedure TWinForm.InitializeComponent;
begin
  //
  // TWinForm
  //
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  Self.ClientSize := System.Drawing.Size.Create(292, 266);
  Self.Name := 'TWinForm';
  Self.Text := 'WinForm';
  Include(Self.Load, Self.TWinForm_Load);
end;
{ENDREGION}
 
procedure TWinForm.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;
 
constructor TWinForm.Create;
begin
  inherited Create;
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent;
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;
 
procedure TWinForm.TWinForm_Load(sender: System.Object; e: System.EventArgs);
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  DirInfo    : System.IO.DirectoryInfo;
  Path       : String;
  Folders    : Array of DirectoryInfo;
  SubFolder1 : System.IO.DirectoryInfo;
  SubFolder2 : System.IO.DirectoryInfo;
  i          : Integer;

begin
  // Create a base folder
  Path := 'C:\Base';
  DirInfo := System.IO.DirectoryInfo.Create(Path);
  DirInfo.&Create;

  // And 2 sub-folders
  SubFolder1 := DirInfo.CreateSubdirectory('SubFolder1');
  SubFolder2 := DirInfo.CreateSubdirectory('SubFolder2');

  // List the folders under C:\Base
  Folders := DirInfo.GetDirectories;

  Console.WriteLine('Listing C:\Base sub folders :');
  Console.WriteLine;

  for i := 0 to Length(Folders)-1 do
    Console.WriteLine(Folders[i].Name);

  // Now move the sub folder to a new name under C:\Base
  SubFolder1.MoveTo('C:\Base\NewFolderName');

  // List the folders under C:\Base
  Folders := DirInfo.GetDirectories;

  Console.WriteLine;
  Console.WriteLine('SubFolder1 renamed to NewFolder');
  Console.WriteLine;
  Console.WriteLine('Listing C:\Base sub folders :');
  Console.WriteLine;

  for i := 0 to Length(Folders)-1 do
    Console.WriteLine(Folders[i].Name);

  // Delete the base and subfolders
  DirInfo.Delete(true);

  Console.Readline;
end.
 
end.
Hide full unit code
  Listing C:\Base sub folders :
  
  SubFolder1
  SubFolder2
  
  SubFolder1 renamed to NewFolder
  
  Listing C:\Base sub folders :
  
  NewFolderName
  SubFolder2
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author