📄 search.cpp
字号:
#include "StdAfx.h"
#include "Search.h"
#include "direct.h"
#include ".\search.h"
Search::Search(CString strFilePath, CString strFileName,CString strToStaticPath)
: type(0)
{
getcwd(m_szOldDir, MAX_PATH); // 保存遍历前的起始目录
if(_chdir((LPCTSTR)strFilePath) == -1) // 进入指定目录
{
//AfxMessageBox("路径错误,请重新设置!");
return;
}
m_strFilePath=strFilePath;
m_strFileName = strFileName;
m_strToStaticPath=strToStaticPath;
} //---------------------------------------------------------------------
void Search::Start()
{
BOOL ans;
CFileFind find;
Run(); // 在每一个目录下执行自定义的操作
ans = find.FindFile("*.*");
while(ans)
{
ans = find.FindNextFile(); // 查找下一个文件
// 查找到的如果是目录并且不是 . 或 ..
if((find.IsDirectory() == TRUE) && (find.IsDots() != TRUE))
{
chdir((LPCTSTR)find.GetFilePath()); // 如果是目录则进入继续查找
Start(); // 递归调用
chdir(".."); // 返回后回到上一层目录继续查找
if(this->type==1)//1为删除目录
rmdir((LPCTSTR)find.GetFilePath());
}
} // End of while
find.Close();
return;
} //---------------------------------------------------------------------
Search::~Search()
{
_chdir(m_szOldDir); // 返回遍历前的起始目录
}
//---------------------------------------------------------------------
void Search::Run()
{
BOOL run_ans;
CFileFind run_find; // m_strFileName 就是 Search 构造函数的第二个参数
run_ans = run_find.FindFile(m_strFileName);
while(run_ans)
{
run_ans = run_find.FindNextFile();
if(run_find.IsDots() != TRUE)
{ // TODO: 请在这里添加你的代码
if(this->type==1 || this->type==2)//1为删除目录,2为清空目录
remove(run_find.GetFilePath());
if(!(run_find.IsDirectory() == TRUE) && this->type==3)
{
CString OldPath=run_find.GetFilePath();
CopyFile(run_find.GetFilePath(),m_strToStaticPath+OldPath.Right(OldPath.GetLength()-m_strFilePath.GetLength()),TRUE);
}
//TRACE("\n%s", run_find.GetFilePath());
}
}
run_find.Close();
return;
}
BOOL Search::JCDir(CString dir)//检查目录的合法性
{
if(_chdir((LPCTSTR)dir) == -1) // 进入指定目录
return FALSE;
else
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -