⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Check if directory exists

  • Hi i do this to check the directory exists:
    if (directorio.exists(Path)){
    and then copy to this directory
    But not Works some solution


    I typically do something like:

        const QFileInfo outputDir("some-directory-path-name");
        if ((!outputDir.exists()) || (!outputDir.isDir()) || (!outputDir.isWritable())) {
            qWarning() << "output directory does not exist, is not a directory, or is not writeable"
                       << outputDir.absoluteFilePath();
            return false;



    you should use this

    QDir pathDir(Path);
    if (pathDir.exists())
    ... do your stuff ...

  • @koahnig i get this message when i do this:
    QIODevice::write (QFile, "PATH"): device not open

    I'm trying to copy one application

  • Have a look at http://doc.qt.io/qt-5/qdir.html. Just like @koahnig said, bool QDir::exists(const QString &name) const and bool QDir::exists() const do different things.

  • You can try this..

    QString destPath;
    QString srcPath;
    QDir dir(destPath);
            // append file name to destPath here
    	if(!QFile::copy(srcPath, destPath))
    		qDebug() << "Unable to copy file!";
    	... copy successful...

    Note that, for QFile::copy to work the destination path should contain the name of the file as well.

    QFile::copy("C:/work/myfolder/srcFile.txt", "D:/samples/destFile.txt");

    will work.

    QFile::copy("C:/work/myfolder/srcFile.txt", "D:/samples");

    won't work.

Log in to reply