(一)采用递归和QDir实现文件夹下所有文件遍历的方法
#include
bool FindFile(const QString & path)
{
QDir dir(path);
if (!dir.exists())
return false;
dir.setFilter(QDir::Dirs|QDir::Files);//除了目录或文件,其他的过滤掉
dir.setSorting(QDir::DirsFirst);//优先显示目录
QFileInfoList list = dir.entryInfoList();//获取文件信息列表
int i = 0;
bool bIsDir;
do{
QFileInfo fileInfo = list.at(i);
if(fileInfo.fileName()=="."|fileInfo.fileName()=="..")
{
i++;
continue;
}
bIsDir = fileInfo.isDir();
if (bIsDir)
{
nFiles++; //对文件属性进行处理
//fileInfo.size(),fileInfo.fileName(),fileInfo.path()
FindFile(fileInfo.filePath());
}
else
{
nFiles++;
}
i++;
}
while(i } (二)QT获取文件夹下所有文件数目及名字 path=QFileDialog::getExistingDirectory(NULL, tr("选择文件夹"),"D:\\",QFileDialog::ShowDirsOnly); 注释掉的两行是用来文件过滤用的,如果你要指定文件后缀名,这样写filter<<"*.dat" 就好了,如果要指定多个后缀名,这样写filter<<"*.dat"<<"*.txt"就行了 文件数目:fileInfo->count(); 文件名称:fileInfo->at(i).fileName(); 文件路径(包含文件名):fileInfo->at(i).filePath(); (三)QT遍历目录获取文件信息 QFileInfo 构造函数: exists(): 判断文件是否存在 文件类型: symLinkTarget(): 返回符号链接的文件 注意: #ifdef Q_OS_UNIX QFileInfo info1("/home/bob/bin/untabify"); QFileInfo info2(info1.symLinkTarget()); #endif #ifdef Q_OS_WIN QFileInfo info1("C://Documents and Settings//Bob//untabify.lnk"); QFileInfo info2(info1.symLinkTarget()); #endif path(): 返回文件路径 QFileInfo fileInfo("/home/dipper/xyz.tar.gz"); 日期相关: 权限: 所有者: dir(): 返回父目录 下面三个都返回"." 相对路径和绝对路径 QDir::setCurrent(QDir::rootPath()); QDir::setCurrent("/tmp"); QDir 显示文件系统目录 实例:文件浏览器 //fileviewer.h #include class FileViewer : public QDialog // file information dialogs widget public: public slots: #endif // FILEVIEWER_H //fileviewer.cpp FileViewer::FileViewer(QWidget *parent) currDir = new QLineEdit; fileListWidget = new QListWidget; QVBoxLayout *vbLayout = new QVBoxLayout(this); connect(currDir, SIGNAL(returnPressed()), connect(fileListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem *)), QString root = "c://"; QStringList strings; FileViewer::~FileViewer() } //显示文件信息列表 void FileViewer::showDir(QListWidgetItem * item) void FileViewer::showFiles(QDir dir) void FileViewer::showFiles() //main.cpp int main( int argc, char **argv ) QApplication app(argc, argv); FileViewer *fileviewer = new FileViewer; 绝对路径 dirName(): 返回目录名称 切换目录:cd(str), cdUp() 创建,修改,删除:mkdir(), rename(), rmdir() 获取目录里面的文件,目录等 静态函数 QDir dir("example"); 遍历目录,读写文件 QDir dir = QDir::root(); // "/" 列出当前目录下所有文件,不包括符号链接,按大小排序,升序 int main(int argc, char *argv[]) QFileInfoList list = dir.entryInfoList();
QDir *dir=new QDir(path);
QStringList filter;
//filter<<"*.dat";
//dir->setNameFilters(filter);
QList
获取文件信息:文件名称,路径,大小,创建时间,修改时间,权限等
使用
路径:
UNIX: /home/dipper/file1
Windows: C://dipper//file1
QFileInfo fileInfo(path);
QFileInfo fileInfo;
fileInfo.setFile(path);
size(): 获取文件大小
isFile(), isDir(), isSymLink()
UNIX和Windows(快捷方式)对符号链接处理不一样
UNIX: size()返回链接的目标文件的大小
Windows: size()返回链接文件本身的大小
info1.isSymLink(); // returns true
info1.absoluteFilePath(); // returns "/home/bob/bin/untabify"
info1.size(); // returns 56201
info1.symLinkTarget(); // returns "/opt/pretty++/bin/untabify"
info1.isSymLink(); // returns false
info1.absoluteFilePath(); // returns "/opt/pretty++/bin/untabify"
info1.size(); // returns 56201
info1.isSymLink(); // returns true
info1.absoluteFilePath(); // returns "C:/Documents and Settings/Bob/untabify.lnk"
info1.size(); // returns 743
info1.symLinkTarget(); // returns "C:/Pretty++/untabify"
info1.isSymLink(); // returns false
info1.absoluteFilePath(); // returns "C:/Pretty++/untabify"
info1.size(); // returns 63942
fileName(): 返回文件名称
fileInfo.path(); // returns "/home/dipper"
fileInfo.fileName(); // returns "xyz.tar.gz"
fileInfo.baseName(); // returns "xyz"
fileInfo.completeBaseName(); // returns "xyz.tar"
fileInfo.suffix(); // returns "gz"
fileInfo.completeSuffix(); // returns "tar.gz"
created(): 创建时间
lastModified(): 最近修改时间
lastRead(): 最近读时间
isReadable(), isWritable(), isExecutable()
ower(), owerId(), group(), groupId(), permissions(),
permission(QFile::Permissions permissions)
下面三个都返回"~/examples/191697"
QFileInfo fileInfo1("~/examples/191697/.");
QFileInfo fileInfo2("~/examples/191697/..");
QFileInfo fileInfo3("~/examples/191697/main.cpp");
QFileInfo fileInfo4(".");
QFileInfo fileInfo5("..");
QFileInfo fileInfo6("main.cpp");
QString absolute = "/local/bin";
QString relative = "local/bin";
QFileInfo absFile(absolute);
QFileInfo relFile(relative);
// absFile and relFile now point to the same file
// absFile now points to "/local/bin",
// while relFile points to "/tmp/local/bin"
#ifndef FILEVIEWER_H
#define FILEVIEWER_H
{
Q_OBJECT
public:
FileViewer(QWidget *parent=0);
~FileViewer();
public:
QLineEdit* currDir;
QListWidget* fileListWidget;
void showFileInfoList(QFileInfoList list);
void showDir(QListWidgetItem * item);
void showFiles(QDir dir);
void showFiles();
};
#include "fileviewer.h"
: QDialog(parent)
{
setWindowTitle(tr("File Viewer"));
currDir->setText("c://");
//currDir->setReadOnly(true);
vbLayout->addWidget(currDir);
vbLayout->addWidget(fileListWidget);
this, SLOT(showFiles()));
this, SLOT(showDir(QListWidgetItem *)));
QDir rootDir(root);
strings << "*" ;
QFileInfoList list = rootDir.entryInfoList(strings);
showFileInfoList(list);
}
{
void FileViewer::showFileInfoList(QFileInfoList list)
{
fileListWidget->clear();
for(int i=0; i
QFileInfo tmpFileInfo = list.at(i);
if((tmpFileInfo.isDir()))
{
QIcon icon(":/images/dir.png");
QString fileName = tmpFileInfo.fileName();
QListWidgetItem *tmp = new QListWidgetItem(icon, fileName); //在列表上显示图标和名称
fileListWidget->addItem(tmp);
}
else if(tmpFileInfo.isFile())
{
QIcon icon(":/images/file.png");
QString fileName = tmpFileInfo.fileName();
QListWidgetItem *tmp = new QListWidgetItem (icon, fileName);
fileListWidget->addItem(tmp);
}
}
}
{
QString str = item->text();
QDir dir;
dir.setPath(currDir->text()); //设置目录
dir.cd(str); //切换到另一个目录
currDir->setText(dir.absolutePath());
showFiles(dir);
}
{
QStringList strings;
strings << "*" ;
QFileInfoList list = dir.entryInfoList(strings, //文件名称过滤器,"*", "*.tar.gz"
QDir::AllEntries, //文件属性过滤器,目录,文件,读写
QDir::DirsFirst //排序方式,名称,修改时间,大小,目录优先
);
showFileInfoList(list);
}
{
QDir dir(currDir->text());
showFiles(dir);
}
#include "fileviewer.h"
#include
{
QFont font("ZYSong18030",12);
QApplication::setFont(font);
QTranslator translator;
translator.load("fileviewer_zh","..");
app.installTranslator(&translator);
fileviewer->show();
return app.exec();
}
QDir("/home/user/Documents")
QDir("C:/Documents and Settings")
QDir("Documents/Letters/Applications").dirName() // "Applications"
QDir().dirName() // "."
count(): 返回条目的数量
QStringList list = dir.entryList();
QFileInfoList fileInfoList = dir.entryInfoList();
Dir Qstring
current(), currentPath()
home(), homePath()
root(), rootPath()
temp(), tempPath()
if (!dir.exists())
qWarning("Cannot find the example directory");
//使用静态函数:QFile::exists("example");
if (!dir.cd("tmp")) { // "/tmp"
qWarning("Cannot find the /"/tmp/" directory");
} else {
QFile file(dir.filePath("ex1.txt")); // "/tmp/ex1.txt"
if (!file.open(QIODevice::ReadWrite))
qWarning("Cannot create the file %s", file.name());
}
#include
#include
{
QCoreApplication app(argc, argv);
QDir dir;
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
dir.setSorting(QDir::Size | QDir::Reversed);
std::cout << " Bytes Filename" << std::endl;
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
std::cout << qPrintable(QString("%1 %2").arg(fileInfo.size(), 10)
.arg(fileInfo.fileName()));
std::cout << std::endl;
}
return 0;
}
QString base
= fi
.baseName();
// base = "archive"
QString base
= fi
.completeBaseName();
// base = "archive.tar"
QString ext= fi.suffix(); // ext = "gz"
QString ext
= fi
.completeSuffix();
// ext = "tar.gz"