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 |
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.
| Show full unit code | Listing C:\Base sub folders :
SubFolder1
SubFolder2
SubFolder1 renamed to NewFolder
Listing C:\Base sub folders :
NewFolderName
SubFolder2
|
|
|
|