VC++ 全盘搜索指定文件,如果存在获取完整路径!
发布网友
发布时间:2024-10-24 13:10
我来回答
共1个回答
热心网友
时间:2024-10-28 13:17
哈哈,前段时间才写了个类似程序,他的功能更加复杂,就随便把代码改改给你啦
这个代码是该的,所以你还是可以优化下性能的(win7下第一次跑很慢的)
上面写了一大段代码,使用方法全部在Test函数中。
懒得写注释了,也没啥可注释的,有啥不懂的直接问。
# include <vector>
# include <algorithm>
struct FileNameAndTime
{
wchar_t szPath[MAX_PATH]; //file directory
wchar_t szName[MAX_PATH]; //file name
FILETIME lastAcc; //last access time
FileNameAndTime()
{
memset(&lastAcc, 0, sizeof(lastAcc));
memset(szName, 0, sizeof(wchar_t) * MAX_PATH);
memset(szPath, 0, sizeof(wchar_t) * MAX_PATH);
}
FileNameAndTime(const PWCHAR fn, const PWCHAR pa, const LPFILETIME ft)
{
if( (0 == fn) || (0 == pa) || (0 == ft) )
return;
memcpy(&lastAcc, ft, sizeof(lastAcc));
wcscpy(szName, fn);
wcscpy(szPath, pa);
}
FileNameAndTime(const FileNameAndTime& fnd)
{
memcpy(&this->lastAcc, &fnd.lastAcc, sizeof(this->lastAcc));
wcscpy(this->szName, fnd.szName);
wcscpy(this->szPath, fnd.szPath);
}
const FileNameAndTime& operator=(const FileNameAndTime& fnd)
{
if(this != &fnd) {
memcpy(&this->lastAcc, &fnd.lastAcc, sizeof(this->lastAcc));
wcscpy(this->szName, fnd.szName);
wcscpy(this->szPath, fnd.szPath);
}
return *this;
}
void GetFullPath( wchar_t (&fp)[MAX_PATH] ) const
{
wcscpy(fp, szPath);
wcscat(fp, szName);
}
friend bool operator>(const FileNameAndTime& l, const FileNameAndTime& r); //compare this object by access time
};
bool operator<(const FileNameAndTime& l, const FileNameAndTime& r) //for sort
{
if(l.lastAcc.dwHighDateTime < r.lastAcc.dwHighDateTime)
return true;
else if (l.lastAcc.dwHighDateTime == r.lastAcc.dwHighDateTime)
{
if(l.lastAcc.dwLowDateTime < r.lastAcc.dwLowDateTime)
return true;
}
return false;
}
class FileOfDirectory
{
private:
static const wchar_t szDot[];
static const wchar_t szDotDot[];
static const wchar_t cStar;
static const wchar_t cSlash;
private:
std::vector<FileNameAndTime> vecFT;
wchar_t szCurrentPath[MAX_PATH];
wchar_t szSearchFileName[MAX_PATH];
private:
void validatePath(const wchar_t* pPath)
{
wcscpy(szCurrentPath, pPath);
int len = wcslen(szCurrentPath);
if( (cStar != szCurrentPath[len - 1])
&& (cSlash != szCurrentPath[len - 2]) )
{
szCurrentPath[len] = cSlash;
szCurrentPath[len + 1] = cStar;
szCurrentPath[len + 2] = 0;
return;
}
if( (cStar != szCurrentPath[len - 1])
&& (cSlash == szCurrentPath[len - 2]) )
{
szCurrentPath[len] = cStar;
szCurrentPath[len + 1] = 0;
return;
}
}
void detectFiles(const LPWSTR szDir)
{
WIN32_FIND_DATA ffd;
HANDLE hFind = ::FindFirstFile(szDir, &ffd);
if (INVALID_HANDLE_VALUE == hFind)
return ;
do
{
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if( (0 == wcscmp(ffd.cFileName, szDot)) || (0 == wcscmp(ffd.cFileName, szDotDot)))
continue;
else
{
wchar_t szTempPath[MAX_PATH];
wcscpy(szTempPath, szDir);
szTempPath[wcslen(szTempPath) - 1] = 0;
wcscat(szTempPath, ffd.cFileName);
int len = wcslen(szTempPath);
szTempPath[len] = cSlash;
szTempPath[len + 1] = cStar;
szTempPath[len + 2] = 0;
detectFiles(szTempPath);
}
}
else {
wchar_t path[MAX_PATH];
wcscpy(path, ffd.cFileName);
CharLower(path);
if(0 == wcscmp(path,szSearchFileName))
{
wcscpy(path, szDir);
path[wcslen(szDir) - 1] = 0;
vecFT.push_back(FileNameAndTime(ffd.cFileName,path, &ffd.ftLastAccessTime));
}
}
}
while (::FindNextFile(hFind, &ffd) != 0);
}
public:
FileOfDirectory(const LPWSTR szDir)
{
validatePath(szDir);
detectFiles(szCurrentPath);
}
FileOfDirectory()
{
static const wchar_t target[] = L"ent.cpp";
wcscpy(szSearchFileName, target);
CharLower(szSearchFileName);
wchar_t diskName[] = L"A:";
ULONG uDriveMask = _getdrives();
for(int i = 0; i < sizeof(ULONG); ++i)
{
if( 0 != (uDriveMask & 1) )
{
validatePath(diskName);
detectFiles(szCurrentPath);
}
++diskName[0];
uDriveMask >>= 1;
}
}
int NumOfFiles() const { return vecFT.size(); }
FileNameAndTime& operator[](int index)
{
if( (index >= 0) && (index < vecFT.size()) )
return vecFT[index];
}
};
const wchar_t FileOfDirectory::szDot[] = L".";
const wchar_t FileOfDirectory::szDotDot[] = L"..";
const wchar_t FileOfDirectory::cStar = L'*';
const wchar_t FileOfDirectory::cSlash = L'\\';
void Test()
{
FileOfDirectory fd;
int num = fd.NumOfFiles();
int szLen = 200 * MAX_PATH;
wchar_t *pstr = new wchar_t[szLen];
memset(pstr, 0, sizeof(wchar_t) * szLen);
wchar_t temp[MAX_PATH];
for(int i = 0; i < num; ++i) {
fd[i].GetFullPath(temp);
wcscat(pstr, temp);
wcscat(pstr, L"\r\n");
}
::MessageBox(0, pstr, 0, 0);
delete []pstr;
}