Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • Hi @RIVOPICO,

    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;
        }
    

    Cheers.



  • @RIVOPICO

    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);
    if(dir.exists())
    {
            // append file name to destPath here
            ....
            destPath.append(fileName);
    	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.
    eg,

    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