Switch
Switch

Reputation: 5366

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

In Qt, how do I check if a given folder exists in the current directory?
If it doesn't exist, how do I then create an empty folder?

Upvotes: 181

Views: 174583

Answers (4)

Petrucio
Petrucio

Reputation: 5679

To both check if it exists and create if it doesn't, including intermediaries:

QDir dir("path/to/dir");
if (!dir.exists())
    dir.mkpath(".");

Upvotes: 200

Vitor Santos
Vitor Santos

Reputation: 137

When you use QDir.mkpath() it returns true if the path already exists, in the other hand QDir.mkdir() returns false if the path already exists. So depending on your program you have to choose which fits better.

You can see more on Qt Documentation

Upvotes: 12

matiasf
matiasf

Reputation: 1118

Why use anything else?

  mkdir(...);

Upvotes: -14

Kyle Lutz
Kyle Lutz

Reputation: 8036

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

QDir("Folder").exists();

To create a new folder named "MyFolder" use:

QDir().mkdir("MyFolder");

Upvotes: 250

Related Questions