MFC 拷贝目录

封装成类FileOP

//cpp file

#include "FileOP.h"

BOOL FileOP::CopyDir(CString srcDirPath,CString destDirPath,BOOL bFailExists) {
	CFileFind ff;
	TRACE(srcDirPath);
	TRACE(destDirPath);

	if(srcDirPath.Right(1) != "\\")
		srcDirPath += "\\";
	if(destDirPath.Right(1) != "\\")
		destDirPath += "\\";
	//judement if srcDirPath&destDirPath exists
	if(!DirExists(srcDirPath.Left(srcDirPath.GetLength()-1))) {
		return FALSE;
	}
	if(!DirExists(destDirPath.Left(destDirPath.GetLength()-1))) {
		return FALSE;
	}
	//get src directory name
	CString tname = srcDirPath;
	tname.Delete(tname.GetLength()-1,1);
	int nameLen = tname.GetLength()-(tname.ReverseFind('\\')+1);
	CString name = tname.Right(nameLen);
	destDirPath += name + "\\";
	CreateDir(destDirPath);

	BOOL bRet = FALSE;
	BOOL bFound = ff.FindFile(srcDirPath+"*");
	while(bFound) {
		bFound = ff.FindNextFile();
		if(ff.GetFileName() == "." || ff.GetFileName() == "..")
			continue;
		if(ff.IsDirectory()) {
			bRet = CopyDir(ff.GetFilePath(),destDirPath);
		}else{
			bRet = CopyFile(ff.GetFilePath(),destDirPath+ff.GetFileName(),bFailExists);
		}
		if(!bRet)
			break;
	}
	ff.Close();
	return bRet;
}

BOOL FileOP::DirExists(CString dirPath) {
	WIN32_FIND_DATA fd;
	BOOL ret = FALSE;
	HANDLE hFind = FindFirstFile(dirPath, &fd);
	if ((hFind != INVALID_HANDLE_VALUE) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
	{
		//目录存在
		ret = TRUE;
	}
	FindClose(hFind);
	return ret;
}

BOOL FileOP::CreateDir(CString dirPath) {
	SECURITY_ATTRIBUTES sa;
	sa.bInheritHandle = FALSE;
	sa.lpSecurityDescriptor = NULL;
	sa.nLength = sizeof(SECURITY_ATTRIBUTES);
	return ::CreateDirectory(dirPath,&sa);
}

header file

#if !defined __FILLOP_H
#define __FILLOP_H

#include "stdafx.h"

class FileOP {
public:
	FileOP(){};
	~FileOP(){};

	static BOOL CopyDir(CString srcDirPath,CString destDirPath,BOOL bFailExists = FALSE);
	static BOOL DirExists(CString dirPath);
	static BOOL CreateDir(CString dirPath);
};

#endif



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值