1、最近要判断某文件夹下,是否存在wps打开的word的文件,基本思路是,利用wps打开word文件时,会在同级目录下生成一个以 .~ 开头的临时文件,word文件关闭后,临时文件自动会被删除。所以只要判断目录下,是否存在以 .~ 开头的文件即可。
2、代码实现方法
#include <QDateTime>
#include <QString>
#include <QVariant>
#include <QtMath>
#include <QCoreApplication>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
using namespace std;
bool FileExisted(string keyword)
{
// 获取文件路径
QString Path = QCoreApplication::applicationDirPath();
int first = Path.lastIndexOf("/");
Path = Path.left(first);
QString filepath = Path.append("/data/report/");
struct dirent* ptr = 0;
DIR *pDir = opendir(filepath.toStdString().c_str());
if(!pDir)
return false;
// 循环读取文件名
while((ptr = readdir(pDir)) != 0)
{
if ((strcmp(ptr->d_name, ".") == 0) || (strcmp(ptr->d_name, "..") == 0))
continue;
// strstr如果没有找到,则会返回nullptr
if (strstr(ptr->d_name, keyword.c_str()) != nullptr) {
return true;
}
}
closedir(pDir);
return false;
}
3、调用
if (FileExisted(".~")) {
// 存在wps打开的文件时,处理以下内容
}