1、遍历一个目录:
CFileFind Finder ;
CString strDir = DataDir ; //DataDir为要操作的文件所在目录
if( strDir.Right(1) != "\\" )
strDir += "\\" ;
strDir += "*.*" ;
bool bWorking = Finder.FindFile( strDir ) ;
while( bWorking )
{
bWorking = Finder.FindNextFile() ;
CString DataFile = Finder.GetFilePath() ; //GetFilePath:得到全路径名
if(finder.IsDirectory() && !finder.IsDots()) //如果是目录 { }
else if( !Finder.IsDirectory() && !Finder.IsDots() )//不为目录
{
CString FileTitle = Finder.GetFileTitle() ;//GetFileTitle:得到不带后缀文件名
CString FileName = Finder.GetFileName(); //GetFileName:得到带后缀的文件名
//........
}
}
Finder.Close() ;
2、成员函数的使用
于不常使用CFileFind类的人,对GetfilePath()和GetFileName()等函数得到的值很容易混淆,我写了一段代码,看执行后的结果便可知道各函数返回的结果(看不懂没关系,后面还有个例子)。
l_strFilePath = ff.GetFilePath();
l_strFileName = ff.GetFileName();
l_nPoint = l_strFileName.ReverseFind('.'); // 因为文件名中可能出现多个'.'所以用 ReverseFind而不用Find?
l_nLength = l_strFileName.GetLength();
l_strFileExt = l_strFileName.Right(l_nLength - l_nPoint - 1);
l_strFileTitle = ff.GetFileTitle();
l_strFileUrl = ff.GetFileURL();
l_strFileRoot = ff.GetRoot();
l_dwLength = ff.GetLength();
得到的结果:
Filepath: f:/hoho/hzyong2008.doc
FileName: hzyong2008.doc
FileExe: doc
nFileTitle: hzyong2008
FileUrl: file://f:/hoho/hzyong2008.doc
FileRoot: f:/hoho
FileLength: 603648
对照代码与上面的结果便可以知道各个函数的功能了吧!还有些成员函数没介绍比如GetLastWriteTime(CTime&refTime )。
3、文件备份
try{
CopyFile(strFullName, strBackDir + strFileName, FALSE); // copy file
DeleteFile(strFullName);// delete source file
}
catch(CFileException, pEx)
{
AfxMessageBox("UD文件备份失败");
}
END_CATCH
4、常用文件函数:
if(finder.IsDirectory() && !finder.IsDots()) //如果是目录
{}
IsDirectory()判断是否目录
IsDots()) 判断目录是否为“.”或".."
在dos中每个目录下都有缺省的两个目录分别为"."和".."分别表示上一层目录和本层目录.
因此当我们遍历目录树下的文件时要过滤掉这两个缺省目录。
每个文件夹都有下面两个特殊子文件夹:
(1) . 表示本文件夹自己
(2) .. 表示本文件夹的父文件夹
显然,在文件夹遍历的时候,这两个子文件夹需要特殊处理,否则将陷入死循环。
IsDots():就是判断是不是这两个文件夹的一个.
MSDN帮助解释:
CFileFind::IsDots
virtual BOOL IsDots( ) const;
Return Value
Nonzero if the found file has the name "." or "..", which indicates that the found file is actually a diretory. Otherwise 0.
删除文件或文件夹:
第一种方法: 定义一个文件类对象来操作
CFile TempFile;
TempFile.Remove(指定文件名);
第二种方法: 使用系统函数 DeleteFile( LPCSTR filename )删除文件 。_rmdir(),删除目录 ,DeleteDirectory(sTempDir); 删除目录 ,RemoveDirectory(sTempDir);删除目录
eg: DeleteFile( char *tempFileName);
上面提到的删除目录的方法只能删除空目录(即空文件夹),如果目录下有文件或者子目录,就不能删除了,VC里好像没有直接的函数,只能手动写个函数来删除了,下面提供一个删除非空目录的方法:
void GetDirFiles(LPCSTR lszDirPath, CStringArray &ar) { if (NULL == lszDirPath) return; LONGLONG lnSize = 0; CFileFind finder; CString szPath = CString(lszDirPath) + "\*.*"; BOOL bWorking = finder.FindFile(szPath); while (bWorking) { bWorking = finder.FindNextFile(); if (finder.IsDirectory()) { if (finder.IsDots()) continue; GetDirFiles(finder.GetFilePath(), ar); } ar.Add(finder.GetFilePath()); } } for (int i = 0; i < DelFiles.GetSize(); ++i) { const CString &szFile = DelFiles[i]; CString szDir((LPCTSTR)szFile, szFile.ReverseFind('\')); DeleteFile(szFile); RemoveDirectory(szDir); } RemoveDirectory(DelParentPath);
这里只写了函数的一小部分。准确的说,是没有一个标准API或者MFC函数来删除一个非空文件夹的(除了用shell方法,不过此方法有弊端),删除非空文件夹的函数只能是自己写。
函数实现思想:
主要是先删除该待删除文件夹内部的所有文件夹和文件夹内的文件,然后在删除该文件夹。比如我要删除A文件夹,那么A文件夹内如果有B、C文件夹,B文件夹内有1、2、D两个文件和一个文件夹,C文件夹内有3、4、5三个文件。采用深度遍历的思想,这样来记:如果D文件夹没有文件,就删除D文件夹。然后删除B文件夹内的1、2文件,再删除B文件夹。如此下去。
删除非空目录 function如下:(以下测试通过)
BOOL DeleteDirectory(char *DirName) { USES_CONVERSION; CFileFind tempFind; char tempFileFind[200]; sprintf(tempFileFind,"%s\\*.*",DirName); BOOL IsFinded=(BOOL)tempFind.FindFile(A2T(tempFileFind)); while(IsFinded) { IsFinded=(BOOL)tempFind.FindNextFile(); if(!tempFind.IsDots()) { char foundFileName[200]; CString dd=tempFind.GetFileName(); const char* mm =T2A(dd);
//_tcscpy(mm,dd); /*_tcscpy(mm,dd.GetBuffer());*/ strcpy(foundFileName,(const char*)mm); if(tempFind.IsDirectory()) { char tempDir[200]; sprintf(tempDir,"%s\\%s",DirName,foundFileName); DeleteDirectory(tempDir); } else { char tempFileName[200]; sprintf(tempFileName,"%s\\%s",DirName,foundFileName); DeleteFile(A2T(tempFileName)); } } } tempFind.Close(); if(!RemoveDirctory(A2T(DirName))) { MessageBox(0,_T("删除目录失败!","警告信息"),MK_OK); return FALSE; } return TRUE; } //————————测试——————// char *DirName="C:\\aa"; DeleteDirectory(DirName);