Last active
February 25, 2017 22:27
-
-
Save julianguarinautoformax/4f5dfb1ebefde2e138c8a6564c70aecc to your computer and use it in GitHub Desktop.
QDir & QFileInfo information
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* QFileInfo: | |
* http://doc.qt.io/qt-5/qfileinfo.html | |
* QDir: | |
* http://doc.qt.io/qt-5/qdir.html | |
*/ | |
#include <QCoreApplication> | |
#include <QDebug> | |
#include <QDir> | |
#include <QFile> | |
#include <QDateTime> | |
#include <QFileDevice> | |
#include <QJsonObject> | |
#define DIRPATH "/Users/foo/foo2/foodirpath" | |
void ls(QDir & d){ | |
qDebug()<<"QString absolutePath() const:"<<d.absolutePath(); | |
qDebug()<<"QString canonicalPath() const:"<<d.canonicalPath(); | |
qDebug()<<"uint count() const:"<<d.count(); | |
qDebug()<<"QString dirName() const:"<<d.dirName(); | |
qDebug()<<"bool exists() const:"<<d.exists(); | |
qDebug()<<"bool isAbsolute() const:"<<d.isAbsolute(); | |
qDebug()<<"bool isReadable() const:"<<d.isReadable(); | |
qDebug()<<"bool isRelative() const:"<<d.isRelative(); | |
qDebug()<<"bool isRoot() const:"<<d.isRoot(); | |
/* | |
*/ | |
qDebug() <<"--- Regular ls: QStringList entryList(const QStringList &nameFilters, Filters filters = NoFilter, SortFlags sort = NoSort) const ---"; | |
QStringList listDir = d.entryList(); | |
auto display0 = [&listDir](){for(QStringList::const_iterator i = listDir.begin();i!=listDir.end();++i){qDebug()<<"--- "<<*i;}}; | |
display0(); | |
qDebug() <<"--- Filtered ls (c*): QStringList entryList(Filters filters = NoFilter, SortFlags sort = NoSort) const ---"; | |
QStringList nfltrs; | |
nfltrs << "c*"; | |
d.setNameFilters(nfltrs); | |
qDebug() <<"Filters QDir::filter() const:"<<d.filter(); | |
listDir = d.entryList(nfltrs); | |
display0(); | |
/*QFileInfoList entryInfoList(const QStringList &nameFilters, Filters filters = NoFilter, SortFlags sort = NoSort) const | |
QFileInfoList entryInfoList(Filters filters = NoFilter, SortFlags sort = NoSort) const | |
*/ | |
QFileInfoList fil = d.entryInfoList(); | |
auto display1 = [&fil](){ | |
QList<QFileInfo>::const_iterator i; | |
for(i = fil.begin(); | |
i!=fil.end(); | |
++i){ | |
QFileInfo info = *i; | |
qDebug()<<"**********************************************"; | |
qDebug()<<"QDir absoluteDir() const: "<<info.absoluteDir(); | |
qDebug()<<"QString absoluteFilePath() const: "<<info.absoluteFilePath(); | |
qDebug()<<"QString absolutePath() const: "<<info.absolutePath(); | |
qDebug()<<"QString baseName() const: "<<info.baseName(); | |
qDebug()<<"QString bundleName() const: "<<info.bundleName(); | |
qDebug()<<"bool caching() const: "<<info.caching(); | |
qDebug()<<"QString canonicalFilePath() const: "<<info.canonicalFilePath(); | |
qDebug()<<"QString canonicalPath() const: "<<info.canonicalPath(); | |
qDebug()<<"QString completeBaseName() const: "<<info.completeBaseName(); | |
qDebug()<<"QString completeSuffix() const: "<<info.completeSuffix(); | |
qDebug()<<"QDateTime created() const: "<<info.created(); | |
qDebug()<<"QDir dir() const: "<<info.dir(); | |
qDebug()<<"bool exists() const: "<<info.exists(); | |
qDebug()<<"QString fileName() const: "<<info.fileName(); | |
qDebug()<<"QString filePath() const: "<<info.filePath(); | |
qDebug()<<"QString group() const: "<<info.group(); | |
qDebug()<<"uint groupId() const: "<<info.groupId(); | |
qDebug()<<"bool isAbsolute() const: "<<info.isAbsolute(); | |
qDebug()<<"bool isBundle() const: "<<info.isBundle(); | |
qDebug()<<"bool isDir() const: "<<info.isDir(); | |
qDebug()<<"bool isExecutable() const: "<<info.isExecutable(); | |
qDebug()<<"bool isFile() const: "<<info.isFile(); | |
qDebug()<<"bool isHidden() const: "<<info.isHidden(); | |
qDebug()<<"bool isNativePath() const :"<<info.isNativePath(); | |
qDebug()<<"bool isReadable() const :"<<info.isReadable(); | |
qDebug()<<"bool isRelative() const :"<<info.isRelative(); | |
qDebug()<<"bool isRoot() const :"<<info.isRoot(); | |
qDebug()<<"bool isSymLink() const :"<<info.isSymLink(); | |
qDebug()<<"bool isWritable() const :"<<info.isWritable(); | |
qDebug()<<"QDateTime lastModified() const :"<<info.lastModified(); | |
qDebug()<<"QDateTime lastRead() const :"<<info.lastRead(); | |
//qDebug()<<"bool makeAbsolute() :"<<info. | |
qDebug()<<"QString owner() const :"<<info.owner(); | |
qDebug()<<"uint ownerId() const :"<<info.ownerId(); | |
qDebug()<<"QString path() const :"<<info.path(); | |
//qDebug()<<"bool permission(QFile::Permissions permissions) const :"<<info. | |
QFile::Permissions p = info.permissions(); | |
QString ps = QString(p.testFlag(QFileDevice::ReadUser) ?"r":"-"); | |
ps +=QString(p.testFlag(QFileDevice::WriteUser) ?"w":"-"); | |
ps +=QString(p.testFlag(QFileDevice::ExeUser) ?"x":"-"); | |
ps += QString(p.testFlag(QFileDevice::ReadGroup) ?"r":"-"); | |
ps +=QString(p.testFlag(QFileDevice::WriteGroup) ?"w":"-"); | |
ps +=QString(p.testFlag(QFileDevice::ExeGroup) ?"x":"-"); | |
ps += QString(p.testFlag(QFileDevice::ReadOwner) ?"r":"-"); | |
ps +=QString(p.testFlag(QFileDevice::WriteOwner) ?"w":"-"); | |
ps +=QString(p.testFlag(QFileDevice::ExeOwner ) ?"x":"-"); | |
qDebug()<<"Permissions: "<<ps; | |
qDebug()<<"**********************************************"; | |
} | |
}; | |
display1(); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication a(argc, argv); | |
QDir d(DIRPATH); | |
qDebug()<<DIRPATH<<" exists: "<<d.exists(); | |
ls(d); | |
return a.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code shows how to manipulate QDir and QFileInfo objects.