directoryc++builderc++builder-10.2-tokyo

How to use directory operations in C++Builder?


I am stuck in creating a directory with C++Builder. If you check this here and here, I find examples for my case, but when I try to use them, none of them work for me! For example, the following code for creating a directory, where the edSourcePath->Text value has been defined.

Unfortunately the documentation is not complete.

try
{
    /* Create directory to specified path */
    TDirectory::CreateDirectory(edSourcePath->Text);
}
catch (...)
{
    /* Catch the possible exceptions */
    MessageDlg("Incorrect path", mtError, TMsgDlgButtons() << mbOK, NULL);
    return;
}

The error message says TDirectory is not a class or namespace.

Another question is, how can I pass the source path and directory name by CreateDirectory(edSourcePath->Text)?


Solution

  • What you are seeing is a compile-time error, not a runtime error. The compiler cannot find the definition of the TDirectory class. You need to #include the header file that TDirectory is defined in, eg:

    #include <System.IOUtils.hpp> // <-- add this!
    
    try
    {
        /* Create directory to specified path */
        TDirectory::CreateDirectory(edSourcePath->Text);
    
        // or, if either DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE or
        // NO_USING_NAMESPACE_SYSTEM_IOUTILS is defined, you need
        // to use the fully qualified name instead:
        //
        // System::Ioutils::TDirectory::CreateDirectory(edSourcePath->Text);
    }
    catch (const Exception &e)
    {
        /* Catch the possible exceptions */
        MessageDlg("Incorrect path.\n" + e.Message, mtError, TMsgDlgButtons() << mbOK, NULL);
        return;
    }
    

    Note, however, that TDirectory::CreateDirectory() throws an exception ONLY if the input String is not a valid formatted path. It DOES NOT throw an exception if the actual directory creation fails. In fact, there is no way to detect that condition with TDirectory::CreateDirectory() itself, you would have to check with TDirectory::Exists() afterwards:

    #include <System.IOUtils.hpp>
    
    try
    {
        /* Create directory to specified path */
        String path = edSourcePath->Text;
        TDirectory::CreateDirectory(path);
        if (!TDirectory::Exists(path))
            throw Exception("Error creating directory");
    }
    catch (const Exception &e)
    {
        /* Catch the possible exceptions */
        MessageDlg(e.Message, mtError, TMsgDlgButtons() << mbOK, NULL);
        return;
    }
    

    Otherwise, TDirectory::CreateDirectory() is just a validating wrapper for System::Sysutils::ForceDirectories(), which has a bool return value. So, you could just call that function directly instead:

    #include <System.SysUtils.hpp>
    
    /* Create directory to specified path */
    if (!ForceDirectories(edSourcePath->Text)) // or: System::Sysutils::ForceDirectories(...), if needed
    {
        MessageDlg("Error creating directory", mtError, TMsgDlgButtons() << mbOK, NULL);
        return;
    }