Delphi IOUtils. Accessing and changing Directories and Files in Delphi

New Delphi versions provide TDirectory, TFile and TPath records which help to access and change directories and files in a easy way. Lots of procedures and functions available to use.

So lets see how to use TDirectory, TFile and TPath 

First we have to use following units in Uses clause

Uses
   System.IOUtils, System.Types;

TDirectory - Any directory related operation

1. Create a new Diretory
TDirectory.CreateDirectory('c:\testfolder');

2. Delete an existing directory
TDirectory.Delete('c:\testfolder');

3. Check if Directory exists
TDirectory.Exists('c:\testfolder');

4. Copy a Directory from One folder to another
TDirectory.Copy('c:\Sourcefolder', c:\Destfolder');

5. Move a Directory from One folder to another
TDirectory.Move('c:\Sourcefolder', c:\Destfolder');

5. Find create time, last opened time and last modified time
TDirectory.GetCreationTime('c:\testfolder');
TDirectory.GetLastAccessTime('c:\testfolder')
TDirectory.GetLastWriteTime('c:\testfolder');

6. Get list of in a Directory
procedure TForm1.Button1Click(Sender: TObject);
var
  strFiles: TStringDynArray;
  i: integer;
begin
  strFiles := TDirectory.GetFiles('C:\YourFolder');
  for i := 0  to High(strFiles) do
Memo1.Lines.Add(strFiles[i]);
end;

7. Get list of Folder in Directory
procedure TForm1.Button1Click(Sender: TObject);
var
  strFiles: TStringDynArray;
  i: integer;
begin
  strFiles := TDirectory.GetDirectories('C:\YourFolder');
  for i := 0  to High(strFiles) do
Memo1.Lines.Add(strFiles[i]);
end;

8. Get list of logical drives in our system
procedure TForm1.Button1Click(Sender: TObject);
var
  strFiles: TStringDynArray;
  i: integer;
begin
  strFiles := TDirectory.GetLogicalDrives;
  for i := 0  to High(strFiles) do
Memo1.Lines.Add(strFiles[i]);
end;

9. Get root Directory GetDirectoryRoot
Str1 := TDirectory.GetDirectoryRoot;


TFile - Any file related operation
1. Create a new file TFILE.Create('C:\TEST1.TXT'); TFILE.Create('C:\TEST1.bmp');
2. Delete a file TFILE.Delete('C:\TEST1.TXT');
3. Check Existing file TFILE.Exists('C:\TEST1.TXT');
4. Copy files TFILE.Copy('C:\TEST1.TXT', 'C:\TEST2.TXT');
5. Move a file TFILE.Move('C:\TEST1.TXT', 'C:\TEST2.TXT');
6. Find create time, last opened time and last modified time TFILE.GetCreationTime('C:\TEST1.TXT'); TFILE.GetLastAccessTime('C:\TEST1.TXT') TFILE.GetLastWriteTime('C:\TEST1.TXT');
7. Write some data to file. TFILE.AppendAllText('C:\TEST1.TXT','ABCDEFGHIJKLMNOPQRSTUVWXYZ'); TFILE.WriteAllText('C:\TEST1.TXT','1234567890');
8. Read data from file ShowMessage(TFILE.ReadAllText('C:\TEST1.TXT')); TPath - Any path related operation
1. Check for Derive exists TPath.DriveExists('C:\Testfolder');
2. Get Temp Path TPath.GetTempPath;
3. Get Temp File Name and Random file Name TPath.GetTempFileName; TPath.GetRandomFileName;
4. Get file extension TPath.GetExtension('C:\TEST1.TXT'));
5. Get file name with or without extension TPath.GetFileName('C:\TEST1.TXT')); TPath.GetFileNameWithoutExtension('C:\TEST1.TXT'));
6. Get root path TPath.GetPathRoot('C:\Testfolder')l
7. Get home path TPath.GetHomePath()

Comments

  1. Thank you for posting such useful code snippets!

    ReplyDelete
  2. Perfect! You can use for in cycle and online variables - for me it is more readable:

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    for var dirItem in TDirectory.GetFiles('C:\YourFolder') do
    Memo1.Lines.Add(dirItem);
    end;

    ReplyDelete

Post a Comment

Popular posts from this blog

MS Excel Automation in Delphi

ShellExecute in Delphi

Drawing Shapes in Delphi