Checking If a Folder Exists (And Creating Folders) in Qt, C++

Checking if a folder exists (and creating folders) in Qt, C++

To check if a directory named "Folder" exists use:

QDir("Folder").exists();

To create a new folder named "MyFolder" use:

QDir().mkdir("MyFolder");

check folder exist and create folder in .pro file

this is how i resolved it

exists($$DESTDIR_WIN_CELLTWEAK) {
message("existing")
} else {
QMAKE_POST_LINK += $$quote(mkdir $${DESTDIR_WIN_CELLTWEAK} $$escape_expand(\n\t))
}

i added if with empty and in else created the directory

Qt can't find created directory

Assuming you are using Qt5.

The default constructor for QDir(const QString& path = QString()) creates instance of the QDir pointing to the current working directory. Check the QDir::current() static method.

The QDir::mkdir(const QString& dirName) creates the sub directory in the current the instance points to. The return value is true if the directory was successfully created.

So for your specific case the directory described in path will be created in current working directory of the program. For debugging purposes you can log QDir(path).absolutePath().

Normally while debugging in XCode it sets the current working directory to something like "~/Library/Developer/Xcode/DerivedData/-ddettossvnbbvaarrlgkfotjkeew/Build/Products/" with the "ddettossvnbbvaarrlgkfotjkeew" being generated for the target, project and some other parameters.

If the problem persists please add more data to your question.

Checking if a folder exists

The function is you are looking for is isdir.

qt check if file exists in a directory, if it doesn't prompt the user for its location, then copy the file to the program working directory

Yes this function implementation is correct. But there is few moments

if(!QFile::copy(temppath, file.fileName()))
qDebug() << file.errorString();
  1. Copy function(same as exists) is static and you don`t need temp object - tempfile
  2. There is good practice to check if copy/read/write functions is successful

How do I check for an existing folder in qml?

Do it in Qt (like @Stu-Mackellar already said).

Access Qt methods like this:

http://doc.qt.nokia.com/qt-maemo-4.7/qtbinding.html#calling-c-methods-from-qml

and do the Qt part like described here:

Checking if a folder exists (and creating folders) in Qt, C++

How to create dir if needed from filename of non-existing file in Qt?

Use the following code:

const QString filePath = "C:/foo/bar/file.ini";
QDir().mkpath(QFileInfo(filePath).absolutePath());

This code will automatically create the path to the specified (nonexistent) file.



QFileInfo::absolutePath() extracts the absolute path to the specified file.

QDir::mkpath() creates the previously extracted path.

asking for folder creation in qt

A usual GUI approach to this would be to use QFileDialog::getExistingDirectory(QWidget* parent, const QString& caption, const QString& dir, Options options) to present a directory selection dialog to the user. Have the output of the selection displayed a in QLineEdit, so if the user wants to create a new subdirectory they can append new folder names.

Then as the comments to your question state, use QDir to determine if the directory is existing or not - and make it if it isn't.



Related Topics



Leave a reply



Submit