打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Qt 操作文件(备份,删除,获取大小,个数,整理)封装好的类直接使用

CMyDir.h

#ifndef CMYDIR_H
#define CMYDIR_H

#include <QObject>

class CMyDir : public QObject
{
	Q_OBJECT

public:
	CMyDir();
	~CMyDir();

public:
	//删除文件夹
	static bool deleteCate(QString strDirPath);
    //备份文件夹 strDestPath:备份路径 strSrcPath:文件路径
	static void BackupCate(QString strDestPath,QString strSrcPath);
	//获取文件夹大小(G)
	static float CateSize(QString strDirPath);
    //整理文件夹(小于fSizeStd直接删除)
	static void ArrangeCate(QString strDirPath,float fSizeStd);
	//获取文件个数
	static int GetFileNum(QString strDirPath);

private:

};

#endif // CMYDIR_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

CMyDir.cpp

#include "CMyDir.h"
#include <QDir>
#include <QFileInfoList>
#include <QFileDialog>
#include <QMessageBox>

CMyDir::CMyDir()
	: QObject()
{

}

CMyDir::~CMyDir()
{

}

//删除文件夹
bool CMyDir::deleteCate(QString strDirPath)
{
	QDir dir(strDirPath);
	QFileInfoList fileList = dir.entryInfoList(QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot);
	//删除文件夹内的文件
	for (int i=0;i<fileList.size();++i)
	{
		QFileInfo fileInfo = fileList.at(i);
		if (!dir.remove(fileInfo.absoluteFilePath()))
		{
			QMessageBox::information(NULL,tr("Information"),tr("Deleting [%1] fail!").arg(fileInfo.completeBaseName()));
			return false;
		}
	}
	QFileInfoList dirList = dir.entryInfoList(QDir::AllDirs | QDir::Hidden | QDir::NoDotAndDotDot);
	//递归调用删除文件夹内的文件
	for (int i=0;i<dirList.size();++i)
	{
		deleteCate(dirList.at(i).absoluteFilePath());
	}

	//删除该文件夹
	if (!dir.rmdir(strDirPath))
	{
		QMessageBox::information(NULL,tr("Information"),tr("Deleting [%1] fail!").arg(strDirPath.section('/',-1)));
		return false;
	}
	return true;
}
//备份文件夹
void CMyDir::BackupCate(QString strDestPath,QString strSrcPath)
{
	QDir dir;
	if (!dir.exists(strDestPath))
		dir.mkpath(strDestPath);
	dir.setPath(strSrcPath);
	QFileInfoList fileList = dir.entryInfoList(QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot);
	//复制文件夹内的文件
	for (int i=0;i<fileList.size();++i)
	{
		QFileInfo fileInfo = fileList.at(i);
		if (!QFile::copy(fileInfo.absoluteFilePath(),strDestPath+"\\"+fileInfo.completeBaseName()+"."+fileInfo.completeSuffix()))
		{
			QMessageBox::information(NULL,tr("Information"),tr("Copying [%1] fail!").arg(fileInfo.completeBaseName()));
			return;
		}
	}
	QFileInfoList dirList = dir.entryInfoList(QDir::AllDirs | QDir::Hidden | QDir::NoDotAndDotDot);
	for (int i=0;i<dirList.size();++i)
	{
		QString strDeepDestPath = strDestPath + "/" + dirList.at(i).completeBaseName();
		QString strDeepSrcPath = strSrcPath + "/" + dirList.at(i).completeBaseName();
		BackupCate(strDeepDestPath,strDeepSrcPath);
	}
}
//获取文件夹大小(G)
float CMyDir::CateSize(QString strDirPath)
{
	float fSize = 0;
	QDir dir(strDirPath); 
	foreach(QFileInfo fileInfo,dir.entryInfoList(QDir::Files))  
		fSize+=fileInfo.size();  
	foreach(QString subDir,dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot))  
		fSize+=CateSize(strDirPath+QDir::separator()+subDir)*1024*1024*1024;
	fSize = fSize/(1024*1024*1024);
	return fSize;
}

//获取文件个数(G)
int CMyDir::GetFileNum(QString strDirPath)
{
	int iSize = 0;
	QDir dir(strDirPath); 
	foreach(QFileInfo fileInfo,dir.entryInfoList(QDir::Files))  
		iSize++;  
	foreach(QString subDir,dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot))  
		iSize+=GetFileNum(strDirPath+QDir::separator()+subDir);
	return iSize;
}

//整理文件夹
void CMyDir::ArrangeCate(QString strDirPath,float fSizeStd)
{
	float fSize = CateSize(strDirPath);
	if (fSize>fSizeStd)
	{
		QDir dir(strDirPath);
		dir.setSorting(QDir::Time|QDir::Reversed );
		QFileInfoList fileList = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
		//QString str;
		//foreach(QFileInfo fileInfo,fileList)  
		//	str = fileInfo.completeBaseName();
		if(!deleteCate(strDirPath+QDir::separator()+ fileList.at(0).completeBaseName()))
			return;
		ArrangeCate(strDirPath,fSizeStd);
	}
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117

测试


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->pathlineEdit->setText("E:\\testFile");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_delect_btn_clicked()
{
    QString FilePath = ui->pathlineEdit->text();
    CMyDir::deleteCate(FilePath);
}

void MainWindow::on_backup_btn_clicked()
{
    QString FilePath = ui->pathlineEdit->text();
    CMyDir::BackupCate("E:\\testFile_BackUp",FilePath);
}

void MainWindow::on_getSize_btn_clicked()
{
    QString FilePath = ui->pathlineEdit->text();

    float size = CMyDir::CateSize(FilePath);
}

void MainWindow::on_getCount_btn_clicked()
{
    QString FilePath = ui->pathlineEdit->text();

    int count = CMyDir::GetFileNum(FilePath);
}

void MainWindow::on_arrange_btn_clicked()
{
    QString FilePath = ui->pathlineEdit->text();
//    CMyDir::ArrangeCate(FilePath);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
qt文件操作随笔
QT 创建文件夹||选择一个文件(夹)||拷贝 删除文件或文件夹
QT读取文件夹下的特定文件_qt打开文件夹路径并读取文件
QT如何获取文件夹名及路径,文件名及文件路径
QT重写filedialog文件对话框
C# 接口实例
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服