在VS2008开发环境下,遍历某个磁盘,结果遍历后多出来两个文件夹: RECYCLER, System Volume Information, 如何把这两个文件夹去掉?我的遍历代码是这样的://读取盘符
void CResManager::ReadDisk(CString strDisk)
{
if ("" == strDisk)
{
return;
} CFileFind fileFind;
if (strDisk.Right(1) != "\\")
{
strDisk += "\\*.*";
}
else
{
strDisk += "*.*";
} int nItemCount = 0; m_nFileList.DeleteAllItems(); BOOL bResult = fileFind.FindFile(strDisk);
while (bResult)
{
bResult = fileFind.FindNextFile(); //是单独的文件
if (!fileFind.IsDirectory() && !fileFind.IsDots())
{
CString strFileName = fileFind.GetFileName();
m_nFileList.InsertItem(nItemCount, strFileName, 0);
}
else if (fileFind.IsDots())
{
continue;
}
else
{
//是文件夹
strDisk = fileFind.GetFilePath();
CString strFileName = fileFind.GetFileName();
m_nFileList.InsertItem(nItemCount, strFileName, 0);
} nItemCount++;
} m_nFileList.UpdateData();
}上面的函数中,strDisk传入盘符D:
void CResManager::ReadDisk(CString strDisk)
{
if ("" == strDisk)
{
return;
} CFileFind fileFind;
if (strDisk.Right(1) != "\\")
{
strDisk += "\\*.*";
}
else
{
strDisk += "*.*";
} int nItemCount = 0; m_nFileList.DeleteAllItems(); BOOL bResult = fileFind.FindFile(strDisk);
while (bResult)
{
bResult = fileFind.FindNextFile(); //是单独的文件
if (!fileFind.IsDirectory() && !fileFind.IsDots())
{
CString strFileName = fileFind.GetFileName();
m_nFileList.InsertItem(nItemCount, strFileName, 0);
}
else if (fileFind.IsDots())
{
continue;
}
else
{
//是文件夹
strDisk = fileFind.GetFilePath();
CString strFileName = fileFind.GetFileName();
m_nFileList.InsertItem(nItemCount, strFileName, 0);
} nItemCount++;
} m_nFileList.UpdateData();
}上面的函数中,strDisk传入盘符D:
{
stack<TCHAR*> Dirs;
stack<int> DirDepth;
LPCTSTR lpszPath;
CString tempPath; lpszPath = path; m_ListCtrl2.DeleteAllItems(); TCHAR *tmp=new TCHAR[lstrlen(lpszPath)+1];
lstrcpy(tmp,lpszPath); if(tmp[lstrlen(tmp)-1]=='\\')
tmp[lstrlen(tmp)-1]='\0'; TCHAR szFind[MAX_PATH*2];
TCHAR szFile[MAX_PATH*2];
TCHAR *curdir;
int curdepth=1; //当前文件夹的深度 Dirs.push(tmp);
DirDepth.push(curdepth); for(;!Dirs.empty();)
{
curdir=Dirs.top();
curdepth=DirDepth.top();
Dirs.pop();
DirDepth.pop(); lstrcpy(szFind,curdir);
lstrcat(szFind, "\\*.*"); // 找所有文件
WIN32_FIND_DATA wfd; HANDLE hFind = FindFirstFile(szFind, &wfd); if (hFind != INVALID_HANDLE_VALUE) // 如果找到
{
do
{ if (wfd.cFileName[0] == '.')
continue; // 过滤"." ".." 这两个目录******一定要处理********
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
wsprintf(szFile, "%s\\%s", curdir, wfd.cFileName); //function(szFile); // 如果找到的是目录,则进入此目录进行递归
TCHAR* tmp=new TCHAR[lstrlen(szFile)+2];
lstrcpy(tmp,szFile);
Dirs.push(tmp);
DirDepth.push(curdepth+1);
}
else
{
for(int i=1;i<=curdepth;++i){
tempPath = ScapathName + "\\"+ wfd.cFileName;
//这里处理你要的文件
}
}
} while (FindNextFile(hFind, &wfd)); }// if delete [] curdir; FindClose(hFind); // 关闭查找句柄 }// for()
return TRUE;
}这是我经常使用的代码,保证好用