Qt determines whether the folder path and file exist or not and creates them

Qt determines whether the folder path and file exist or not and creates them

  • Chapter1 Qt determines whether the folder path and file exist or not and creates them
    • Qt determines whether a folder/directory exists
    • Qt determines whether a file exists
  • Chapter2 Qt determines whether a file or folder exists and creates a folder
    • 1. Determine whether the folder exists
    • 2. Determine whether the file exists
    • 3. Determine whether the file or folder exists (that is, it is not sure whether the string is a file or folder path)
    • 4. Determine whether the folder exists, create it if it does not exist
    • 5. The following is an excerpt of other network test codes

Chapter1 Qt determines whether the folder path and file exist or not and creates them

Original link: https://blog.csdn.net/yao_hou/article/details/121389476

Qt determines whether the folder/directory exists

Qt determines whether a folder/directory exists. You can use the exists method of the QDir class to determine. When using it, you need to include the header file #include , such as the following code:

#include <QCoreApplication>
#include <QDir>
#include <QDebug>

///
/// \brief determines whether the folder exists, and creates it if it does not exist.
/// \param fullPath
/// \return
///
bool DirExist(QString fullPath)
{<!-- -->
    QDir dir(fullPath);
    if(dir.exists())
    {<!-- -->
        //The current folder exists
        return true;
    }
    else
    {<!-- -->
        //Create if it does not exist
        bool ok = dir.mkdir(fullPath); //Only create a first-level subdirectory, that is, the upper-level directory must exist
        return ok;
    }
}

///
/// \brief determines whether the folder exists. If it does not exist, create it. Multi-level directories can be created.
/// \param fullPath
/// \return
///
bool DirExistEx(QString fullPath)
{<!-- -->
    QDir dir(fullPath);
    if(dir.exists())
    {<!-- -->
        return true;
    }
    else
    {<!-- -->
        //The current directory does not exist, create it, you can create multi-level directories
        bool ok = dir.mkpath(fullPath);
        return ok;
    }
}


int main(int argc, char *argv[])
{<!-- -->
    QCoreApplication a(argc, argv);

    bool isOk = DirExist("D:/1234");
    qDebug() << isOk;

    isOk = DirExistEx("D:/1/2/3");
    qDebug() << isOk;

    return a.exec();
}

  • DirExist function, determines whether the folder exists, and creates it if it does not exist.
  • DirExistEx function, determines whether the folder exists, creates it if it does not exist, and can create multi-level directories

The difference between the two is the method of creating a folder

  • QDir mkdir: Create a directory (folder/path)
  • QDir mkpath: Create multi-level directories

Qt determines whether the file exists

code show as below:

#include <QCoreApplication>
#include <QFile>
#include <QDebug>

///
/// \brief determines whether the file exists. If it does not exist, create the file.
/// \param fullFileName
/// \return
///
bool FileExist(QString fullFileName)
{<!-- -->
    QFile file(fullFileName);

    if(file.exists())
    {<!-- -->
        return true;
    }
    else
    {<!-- -->
        qDebug() << u8"The file does not exist, then create a new file";
        file.open( QIODevice::ReadWrite | QIODevice::Text );
\t\t
//Pay attention to closing the file
        file.close();
    }

    return false;
}


int main(int argc, char *argv[])
{<!-- -->
    QCoreApplication a(argc, argv);

    bool isOk = FileExist("D:/1234567.ini"); //Text type can create txt, ini, json, xml, etc.
    qDebug() << isOk;

    return a.exec();
}

When the file does not exist, calling the open method of QFile is equivalent to creating a file.

Chapter2 Qt determines whether a file or folder exists and creates a folder

Original link: https://blog.csdn.net/lusirking/article/details/51644782

1. Determine whether the folder exists

Parameter Description:

QString fullPath;//Full path of folder
/*method 1*/
bool isDirExist(QString fullPath)
{<!-- -->
    QDir dir(fullPath);
    if(dir.exists())
    {<!-- -->
      return true;
    }
    return false;
}
/*Method 2*/
bool isDirExist(QString fullPath)
{<!-- -->
    QFileInfo fileInfo(fullPath);
    if(fileInfo.isDir())
    {<!-- -->
      return true;
    }
    return false;
}

2. Determine whether the file exists

Parameter Description:

QString fullFileName;//Full path of the file (including file name)
/*method 1*/
bool isFileExist(QString fullFileName)
{<!-- -->
    QFileInfo fileInfo(fileFullName);
    if(fileInfo.isFile())
    {<!-- -->
        return true;
    }
    return false;
}

3. Determine whether the file or folder exists (that is, it is not sure whether the string is a file or folder path)

Parameter Description:

QString fullFilePath;//path name
/*method 1*/
bool isFileExist(QString fullFilePath)
{<!-- -->
    QFileInfo fileInfo(fullFilePath);
    if(fileInfo.exists())
    {<!-- -->
        return true;
    }
    return false;
}
/*Method 2*/
bool isFileExist(QString fullFilePath)
{<!-- -->
    QFile file(fullFilePath);
    if(file.exists())
    {<!-- -->
        return true;
    }
    return false;
}

4. Determine whether the folder exists, create it if it does not exist

/*Method 1*/
bool isDirExist(QString fullPath)
{<!-- -->
    QDir dir(fullPath);
    if(dir.exists())
    {<!-- -->
      return true;
    }
    else
    {<!-- -->
       bool ok = dir.mkdir(fullPath);//Only create a first-level subdirectory, that is, the upper-level directory must exist
       return ok;
    }
}
/*method 1*/
bool isDirExist(QString fullPath)
{<!-- -->
    QDir dir(fullPath);
    if(dir.exists())
    {<!-- -->
      return true;
    }
    else
    {<!-- -->
       bool ok = dir.mkpath(fullPath);//Create a multi-level directory
       return ok;
    }
}

5. The following is an excerpt of other network test codes

{<!-- -->
    QFileInfo fi("C:/123"); // Directory exists
    qDebug() << fi.isFile(); // false
    qDebug() << fi.isDir(); // true
    qDebug() << fi.exists(); // true
    qDebug() << fi.isRoot(); // false
    qDebug() << QFile::exists("C:/123"); // true
    qDebug() << QDir("C:/123").exists(); // true

    fi.setFile("C:/ABC"); // The directory does not exist
    qDebug() << fi.isFile(); // false
    qDebug() << fi.isDir(); // false
    qDebug() << fi.exists(); // false
    qDebug() << fi.isRoot(); // false
    qDebug() << QFile::exists("C:/ABC"); // false
    qDebug() << QDir("C:/ABC").exists(); // false

    fi.setFile("C:/"); // Existing drive
    qDebug() << fi.isFile(); // false
    qDebug() << fi.isDir(); // true
    qDebug() << fi.exists(); // true
    qDebug() << fi.isRoot(); // true
    qDebug() << QFile::exists("C:/"); // true
    qDebug() << QDir("C:/").exists(); // true

    fi.setFile("Z:/"); // Non-existent drive
    qDebug() << fi.isFile(); // false
    qDebug() << fi.isDir(); // false
    qDebug() << fi.exists(); // false
    qDebug() << fi.isRoot(); // false
    qDebug() << QFile::exists("Z:/"); // false
    qDebug() << QDir("Z:/").exists(); // false

    fi.setFile("C:/123.lnk"); // The shortcut exists and the file pointed to also exists
    qDebug() << fi.isFile(); // true
    qDebug() << fi.isDir(); // false
    qDebug() << fi.exists(); // true
    qDebug() << fi.isRoot(); // false
    qDebug() << QFile::exists("C:/123.lnk"); // true
    qDebug() << QDir("C:/123.lnk").exists(); // false

    fi.setFile("C:/456.lnk"); // The shortcut exists but the file pointed to does not exist
    qDebug() << fi.isFile(); // false
    qDebug() << fi.isDir(); // false
    qDebug() << fi.exists(); // false
    qDebug() << fi.isRoot(); // false
    qDebug() << QFile::exists("C:/456.lnk"); // false
    qDebug() << QDir("C:/456.lnk").exists(); // false
}

As you can see, what is easily confusing is the exists method. This method is a general judgment method and can be regarded as an expression like this
exists() == (isFile() || isDir())

That is to say, it is not rigorous to simply use the exists method to determine whether a file or folder exists
For example, your original intention is to determine whether the file exists, but the file does not exist, and there happens to be a folder with the same name, then exists will also return true. The same goes for folders

A summary based on the above code

Accurately determine whether the file exists

1. Use the QFileInfo::isFile() method

Accurately determine whether the folder exists

1. Use the QFileInfo::isDir() method
2. Use the QDir::exists() method

Not sure if the string is a file or folder path

1. Use the QFileInfo::exists() method
2. Use the QFile::exists() method