PathOperations (C++)

From RAD Studio XE2 Code Examples
Jump to: navigation, search

Language:

Description

This examples demonstrates the use of methods in TPath class to do operations with files and directories.

Code

void __fastcall TForm1::btnDriveExistsClick(TObject *Sender)
{
        /* Check if drive exists */
        if (TPath::DriveExists(edPath->Text))
                MessageDlg("Drive exists!", mtInformation, TMsgDlgButtons() << mbOK, 0);
        else
                MessageDlg("Not drive found to specified path!", mtInformation, TMsgDlgButtons() << mbOK, 0);
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::btnChangeExtentionClick(TObject *Sender)
{
        /* Change the extention of the given file or directory with the specified extention */
        try
        {
                edPath->Text = TPath::ChangeExtension(edPath->Text, edExtention->Text);
        }
        catch(...)
        {
        /* Catch the possible exceptions */
                MessageDlg("Incorrect path or extention", mtError, TMsgDlgButtons() << mbOK, 0);
        }
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::btnGetDirectoryNameClick(TObject *Sender)
{
        String LDirectoryName;
        /* Extract the directory name from specified parh */
        try
        {
                LDirectoryName = TPath::GetDirectoryName(edPath->Text);
 
                if (LDirectoryName == "")
                        MessageDlg("No directory found at specified path", mtInformation, TMsgDlgButtons() << mbOK, 0);
                else
                        MessageDlg("The directory name is: " + LDirectoryName, mtInformation, TMsgDlgButtons() << mbOK, 0);
        }
        catch(...)
        {
                /* Catch the possible exceptions */
                MessageDlg("Incorrect path!", mtError, TMsgDlgButtons() << mbOK, 0);
        }
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::btnGetFileNameClick(TObject *Sender)
{
        String LFileName;
        /* Extract the file name from specified parh */
        try
        {
                LFileName = TPath::GetFileName(edPath->Text);
 
                if (LFileName == "")
                        MessageDlg("No file found at specified path", mtInformation, TMsgDlgButtons() << mbOK, 0);
                else
                        MessageDlg("The file name is: " + LFileName, mtInformation, TMsgDlgButtons() << mbOK, 0);
        }
        catch(...)
        {
                /* Catch the possible exceptions */
                MessageDlg("Incorrect path!", mtError, TMsgDlgButtons() << mbOK, 0);
        }
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::btnHasExtentionClick(TObject *Sender)
{
        if (TPath::HasExtension(edPath->Text))
                MessageDlg("The file has extention!", mtInformation, TMsgDlgButtons() << mbOK, 0);
        else
                MessageDlg("Incorrect path!", mtError, TMsgDlgButtons() << mbOK, 0);
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::btnGetExtentionClick(TObject *Sender)
{
        String LExtention;
        /* Extract the extention from specified parh */
        try
        {
                LExtention = TPath::GetExtension(edPath->Text);
 
                if (LExtention == "")
                        MessageDlg("The file name has no extention", mtInformation, TMsgDlgButtons() << mbOK, 0);
                else
                        MessageDlg("The extention is: " + LExtention, mtInformation, TMsgDlgButtons() << mbOK, 0);
        }
        catch(...)
        {
                /* Catch the possible exceptions */
                MessageDlg("Incorrect path!", mtError, TMsgDlgButtons() << mbOK, 0);
        }
}
//---------------------------------------------------------------------------

Uses

Personal tools
Previous Versions