Qt开发(4)——QDir类处理文件夹总结

一、获取系统路径

1. C盘目录下的隐藏文件夹

// C:/Users/Administrator/AppData
QDir::homePath()  + "/AppData";
QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/AppData";

2. 桌面路径

// C:/Users/Administrator/Desktop
QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)

3. 软件当前工作目录

// testApp/package 
// D:/BuildTrees/build-my_qml_test-Debug
QDir::currentPath()

4. 动态库路径

//	testApp/package/bin
QCoreApplication::applicationDirPath()

二、复制文件夹

将sourceFolder文件夹内的全部File和Folder, 复制到指定位置destinationFolder

int copyFolder(const QString &sourceFolder, const QString &destinationFolder)
{
    QDir sourceDir(sourceFolder);
    if (!sourceDir.exists())
    {
        qWarning() << "Source path does not exist: " << sourceFolder;
        return -1;
    }
    
    // 遍历文件,进行复制
    QStringList files = sourceDir.entryList(QDir::Files);
    foreach (const QString &file, files)
    {
        QString sourceFilePath = sourceFolder + "/" + file;
        QString destinationFilePath = destinationFolder + "/" + file;
        QFile::copy(sourceFilePath, destinationFilePath);
    }

	// 遍历文件夹,递归复制内部的文件
    QStringList dirs = sourceDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
    foreach (const QString &dir, dirs)
    {
        QString sourceSubDir = sourceFolder + "/" + dir;
        QString destinationSubDir = destinationFolder + "/" + dir;
        copyFolder(sourceSubDir, destinationSubDir);
    }
    return 0;
}

三、删除文件夹

1. 删除指定文件/文件夹

#include <QFile>
#include <QDir>
#include <QString>

bool deleteFileOrFolder(const QString &strPath)//要删除的文件夹或文件的路径
{
	if (strPath.isEmpty() || !QDir().exists(strPath))//是否传入了空的路径||路径是否存在
		return false;
		
	QFileInfo FileInfo(strPath);

	if (FileInfo.isFile())//如果是文件
		QFile::remove(strPath);
	else if (FileInfo.isDir())//如果是文件夹
	{
		QDir qDir(strPath);
		qDir.removeRecursively();
	}
	return true;
}

2. 删除文件夹内部分文件

如果folderPath文件夹内的文件数量超出上限uplimit,则删除最老的几个文件,只保留lowlimit个。

int clearFolder(const QString &folderPath, const int &uplimit, const int &lowlimit)
{
    QDir folder(folderPath);
    if (!folder.exists())
    {
        qWarning() << "folder path does not exist: " << folderPath;
        return -1;
    }

    // 获取文件夹中所有的文件和文件夹,同时排序
    QFileInfoList fileList = folder.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Time);
    if (fileList.count()<= uplimit) {
        qDebug() << "No need to clean the folder.";
        return 0;
    }
    // 删除多余的文件
    for (int i = lowlimit; i < fileCount; ++i) {
        QString filePath = fileList.at(i).absoluteFilePath();
        QDir delFolder(filePath);
        delFolder.removeRecursively();      
    }
    return 0;
}

四、压缩文件/文件夹

Qt开发(1)——解压缩文件的多种方式总结_wangpailiulanqi8的博客-CSDN博客

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值