一、获取文件夹下文件
QStringList DeleteFile::findFiles(const QString &startDir, QStringList filters, bool _isParent)
{
QStringList names;
QDir dir(startDir);
foreach(QString file, dir.entryList(filters, QDir::Files))
names += startDir + '/' + file;
if (_isParent) return names;
foreach(QString subdir, dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot))
names += findFiles(startDir + '/' + subdir, filters, _isParent);
return names;
}
二、删除文件
void DeleteFile::pushButton_Delete()
{
bool isParent = true;
if (!ui.radioButton_Current->isChecked()&& ui.radioButton_Subdirectories->isChecked())
{
isParent = false;
}
if (m_selectfolder.isEmpty()) return;
QStringList names;
names = findFiles(m_selectfolder, m_filters, isParent);
printLogger("文件正在删除...");
for (int i=0;i< names.size();i++)
{
QString strPath = names.at(i);
if (strPath.isEmpty() || !QDir().exists(strPath))
return;
QFileInfo FileInfo(strPath);
if (FileInfo.isFile())
QFile::remove(strPath);
}
printLogger("文件删除完成!");
}
三、拷贝文件
bool DeleteFile::copyDirectoryFiles(const QString &fromDir, const QString &toDir, bool coverFileIfExist)
{
QDir sourceDir(fromDir);
QDir targetDir(toDir);
if (!targetDir.exists()) {
if (!targetDir.mkdir(targetDir.absolutePath()))
return false;
}
QFileInfoList fileInfoList = sourceDir.entryInfoList();
foreach(QFileInfo fileInfo, fileInfoList) {
QString str = fileInfo.fileName();
if (fileInfo.fileName() == "." || fileInfo.fileName() == "..")
continue;
qDebug() << fileInfo.fileName();
if (fileInfo.isDir()) {
qDebug() << "****************";
qDebug() << fileInfo.fileName();
qDebug() << "****************";
bool isCopy = true;
for(int i=0;i<m_CopyFiles.size();i++)
{
if (fileInfo.fileName() == m_CopyFiles.at(i))
isCopy=false;
}
if (!isCopy) continue;
if (!copyDirectoryFiles(fileInfo.filePath(),
targetDir.filePath(fileInfo.fileName()),
coverFileIfExist))
return false;
}
else {
if (coverFileIfExist && targetDir.exists(fileInfo.fileName())) {
targetDir.remove(fileInfo.fileName());
}
if (!QFile::copy(fileInfo.filePath(),
targetDir.filePath(fileInfo.fileName()))) {
return false;
}
}
}
return true;
}