文件及目录遍历
.h
#pragma once
class CFileTool
{
public:
CFileTool();
~CFileTool();
public:
static void DeleteDir(CString DirPath); //删除目录
static void DeleteDirEx(CString DirPath, CString fileType); //DirPath文件目录,删除指定目录的文件,filetype文件后缀
static void DeleteDirEx(CString DirPath,CString fileType,CString fliter);
};
.cpp
#include "StdAfx.h"
#include "FileTool.h"
CFileTool::CFileTool()
{
}
CFileTool::~CFileTool()
{
}
void CFileTool::DeleteDir(CString DirPath)
{
CFileFind ff;
CString strDir, strFile, strTempDir;
strTempDir = DirPath;
if( strTempDir.Right(1) == "\\" )
{
strTempDir = strTempDir.Left( strTempDir.GetLength()-1 );
}
if( !ff.FindFile( strTempDir) )
{
ff.Close();
MessageBox(NULL,"指定的目录不存在!","标题",MB_OK);
return ;
}
strDir = DirPath;
if( strDir.Right(1) != "\\" )
{
strDir += "\\";
}
strDir += "*.*";
BOOL bFind = ff.FindFile(strDir);
while ( bFind )
{
bFind = ff.FindNextFile();
if( ff.IsDots() )
continue;
CString strFileName = ff.GetFileName();
strFile = DirPath;
if( strFile.Right(1) != "\\" )
strFile +="\\";
strFile += strFileName;
if( ff.IsDirectory() )
DeleteDir(strFile);
else
{
if( ff.IsReadOnly() )
SetFileAttributes(strFile, GetFileAttributes(strFile)&(~FILE_ATTRIBUTE_READONLY) );
DeleteFile( strFile );
}
}
ff.Close();
RemoveDirectory(DirPath);
}
void CFileTool::DeleteDirEx(CString DirPath,CString fileType)
{
CFileFind ff;
CString strDir, strFile, strTempDir;
strTempDir = DirPath;
if( strTempDir.Right(1) == "\\" )
{
strTempDir = strTempDir.Left( strTempDir.GetLength()-1 );
}
if( !ff.FindFile(strTempDir) )
{
ff.Close();
MessageBox(NULL,"指定的目录不存在!","标题",MB_OK);
return;
}
strDir = DirPath;
if( strDir.Right(1) != "\\" )
strDir += "\\";
if( fileType.Left(1) == "." )
strDir.Append("*");
else
strDir.Append("*");
strDir += fileType;
BOOL bFind = ff.FindFile( strDir );
while ( bFind )
{
bFind = ff.FindNextFile();
if( ff.IsDots() )
continue;
CString strFileName = ff.GetFileName();
strFile = DirPath;
if( strFile.Right(1) != "\\" )
strFile += "\\";
strFile += strFileName;
if( ff.IsDirectory() )
DeleteDir(strFile);
else
{
if( ff.IsReadOnly() )
SetFileAttributes(strFile, GetFileAttributes(strFile)&(~FILE_ATTRIBUTE_READONLY)); //如果文件为只读属性,取消只读属性
DeleteFile(strFile);
}
}
ff.Close();
}
void CFileTool::DeleteDirEx(CString DirPath,CString fileType,CString fliter)
{
CFileFind ff;
CString strDir, strFile, strTempDir;
strTempDir = DirPath;
if(strTempDir.Right(1) == "\\")
{
strTempDir = strTempDir.Left(strTempDir.GetLength() -1);
}
if(!ff.FindFile(strTempDir))
{
ff.Close();
MessageBox(NULL,"指定的目录不存在!","标题",MB_OK);
return;
}
strDir = DirPath;
if ( strDir.Right(1) != "\\" )
strDir += "\\";
if(fileType.Left(1) == ".")
strDir.Append("*");
else
strDir.Append("*.");
strDir += fileType;
BOOL bFind = ff.FindFile(strDir);
while ( bFind )
{
bFind = ff.FindNextFile();
if ( ff.IsDots() )
continue;
CString strFileName = ff.GetFileName();
if(strFileName.Find(fliter)==-1)
continue;
strFile = DirPath;
if ( strFile.Right(1) != "\\" )
strFile += "\\";
strFile += strFileName;
if ( ff.IsDirectory() )
DeleteDir(strFile);
else
{
if ( ff.IsReadOnly() )
SetFileAttributes(strFile, GetFileAttributes(strFile) &(~FILE_ATTRIBUTE_READONLY) );
DeleteFile(strFile);
}
}
ff.Close();
}
浙公网安备 33010602011771号