Windows 下 MFC 获取文件夹信息(创建、最后一次修改、最后一次写入)、重命名操作、删除操作、拷贝操作、剪切操作

本文介绍了四个实用的Windows API函数:GetFolderInfo用于获取文件夹信息,MoveFolder实现文件夹移动,ReNameFolder负责重命名,DeleteFolder则删除文件夹。CopyFolder函数负责复制文件夹内容。这些函数通过SHFileOperation结构体操作文件系统,提供高效文件管理功能。
摘要由CSDN通过智能技术生成
//函数名称:GetFolderInfo
	//函数参数:lpszPath文件夹路径
	//函数功能:获取文件夹相关信息
	void GetFolderInfo(LPCTSTR lpszPath);

	//函数名称:MoveFolder
	//函数参数:lpszFromPath源文件夹路径;lpszToPath目的文件夹路径
	//函数功能:移动源文件夹及其中文件都到指定的路径下
	bool MoveFolder(LPCTSTR lpszFromPath, LPCTSTR lpszToPath);

	//函数名称:ReNameFolder
	//函数参数:lpszFromPath源文件夹路径;lpszToPath目的文件夹路径
	//函数功能:修改源文件夹的名字
	bool ReNameFolder(LPCTSTR lpszFromPath, LPCTSTR lpszToPath);

	//函数名称:DeleteFolder
	//函数参数:lpszPath要删除的路径指针
	//函数功能:删除指定文件夹以及里面的文件
	bool DeleteFolder(LPCTSTR lpszPath);

	//函数名称:CopyFolder
	//函数参数:lpszFromPath源文件夹路径;lpszToPath目的文件夹路径
	//函数功能:拷贝文件夹机器文件夹中的所有内容到指定路径下
	bool CopyFolder(LPCTSTR lpszFromPath, LPCTSTR lpszToPath);
bool MoveFolder(LPCTSTR lpszFromPath, LPCTSTR lpszToPath)
{
	int nLengthFrm = strlen(lpszFromPath);
	char* NewPathFrm = new char[nLengthFrm + 2];
	strcpy(NewPathFrm, lpszFromPath);
	NewPathFrm[nLengthFrm] = ' ';
	NewPathFrm[nLengthFrm + 1] = ' ';
	SHFILEOPSTRUCT FileOp;
	ZeroMemory((void*)&FileOp, sizeof(SHFILEOPSTRUCT));
	FileOp.fFlags = FOF_NOCONFIRMATION;
	FileOp.hNameMappings = NULL;
	FileOp.hwnd = NULL;
	FileOp.lpszProgressTitle = NULL;
	FileOp.pFrom = NewPathFrm;
	FileOp.pTo = lpszToPath;
	FileOp.wFunc = FO_MOVE;
	return SHFileOperation(&FileOp) == 0;
}

bool ReNameFolder(LPCTSTR lpszFromPath, LPCTSTR lpszToPath)
{
	int nLengthFrm = strlen(lpszFromPath);
	char* NewPathFrm = new char[nLengthFrm + 2]();
	strcpy(NewPathFrm, lpszFromPath);	
	SHFILEOPSTRUCT FileOp;
	ZeroMemory((void*)&FileOp, sizeof(SHFILEOPSTRUCT));
	FileOp.fFlags = FOF_NOCONFIRMATION;
	FileOp.hNameMappings = NULL;
	FileOp.hwnd = NULL;
	FileOp.lpszProgressTitle = NULL;
	FileOp.pFrom = NewPathFrm;
	FileOp.pTo = lpszToPath;
	FileOp.wFunc = FO_RENAME;
	return SHFileOperation(&FileOp) == 0;
}

bool DeleteFolder(LPCTSTR lpszPath)
{
	int nLength = strlen(lpszPath);
	char* NewPath = new char[nLength + 2]();
	strcpy(NewPath, lpszPath);	
	SHFILEOPSTRUCT FileOp;
	ZeroMemory((void*)&FileOp, sizeof(SHFILEOPSTRUCT));
	FileOp.fFlags = FOF_NOCONFIRMATION|FOF_SILENT;		//FOF_SILENT会隐藏进度窗口
	FileOp.hNameMappings = NULL;
	FileOp.hwnd = NULL;
	FileOp.lpszProgressTitle = NULL;
	FileOp.pFrom = NewPath;
	FileOp.pTo = NULL;
	FileOp.wFunc = FO_DELETE;
	return SHFileOperation(&FileOp) == 0;
}

bool CopyFolder(LPCTSTR lpszFromPath, LPCTSTR lpszToPath)
{
	int nLengthFrm = strlen(lpszFromPath);
	char* NewPathFrm = new char[nLengthFrm + 2]();
	strcpy(NewPathFrm, lpszFromPath);
	SHFILEOPSTRUCT FileOp;
	ZeroMemory((void*)&FileOp, sizeof(SHFILEOPSTRUCT));
	FileOp.fFlags = FOF_NOCONFIRMATION;
	FileOp.hNameMappings = NULL;
	FileOp.hwnd = NULL;
	FileOp.lpszProgressTitle = NULL;
	FileOp.pFrom = NewPathFrm;
	FileOp.pTo = lpszToPath;
	FileOp.wFunc = FO_COPY;
	return SHFileOperation(&FileOp) == 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值