SKY外语计算机学习
标题:
复制文件夹.移动文件夹.删除文件夹(查找文件) 的实现(Win32)
[打印本页]
作者:
wangbeacon
时间:
2012-6-12 18:48
标题:
复制文件夹.移动文件夹.删除文件夹(查找文件) 的实现(Win32)
本帖最后由 sky_yx 于 2015-12-30 14:18 编辑
移动文件夹同分区下直接 MoveFile 即可
#include <Windows.h>
BOOL CopyDirectory(LPCTSTR szSourcePath,LPCTSTR szDestPath)
{
//修正查找字符串
DWORD dwPathLen = _tcslen(szSourcePath);
LPTSTR szFindPath = new TCHAR[dwPathLen + 5];
_tcscpy_s(szFindPath,dwPathLen + 5,szSourcePath);
if(szFindPath[dwPathLen - 1] != _T('\\'))
_tcscat_s(szFindPath,dwPathLen + 5,_T("\\"));
_tcscat_s(szFindPath,dwPathLen + 5,_T("*.*"));
CreateDirectory(szDestPath,NULL);
WIN32_FIND_DATA FindData;
HANDLE hFindFile = FindFirstFile(szFindPath,&FindData);
delete szFindPath;
if(hFindFile == INVALID_HANDLE_VALUE)return FALSE;
BOOL bIsSucceeded;
while(FindNextFile(hFindFile,&FindData))
{
if(FindData.cFileName[0] == _T('.'))continue;
//生成路径字符串
DWORD dwFileLen = _tcslen(FindData.cFileName);
LPTSTR szNewSrcPath = new TCHAR[dwPathLen + dwFileLen + 2];
_tcscpy_s(szNewSrcPath,dwPathLen + dwFileLen + 2,szSourcePath);
if(szNewSrcPath[dwPathLen - 1] != _T('\\'))
_tcscat_s(szNewSrcPath,dwPathLen + dwFileLen + 2,_T("\\"));
_tcscat_s(szNewSrcPath,dwPathLen + dwFileLen + 2,FindData.cFileName);
LPTSTR szNewDstPath = new TCHAR[dwPathLen + dwFileLen + 2];
_tcscpy_s(szNewDstPath,dwPathLen + dwFileLen + 2,szDestPath);
if(szNewDstPath[dwPathLen - 1] != _T('\\'))
_tcscat_s(szNewDstPath,dwPathLen + dwFileLen + 2,_T("\\"));
_tcscat_s(szNewDstPath,dwPathLen + dwFileLen + 2,FindData.cFileName);
if(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
bIsSucceeded = CopyDirectory(szNewSrcPath,szNewDstPath);
}
else
{
bIsSucceeded = CopyFile(szNewSrcPath,szNewDstPath,FALSE);
}
delete szNewSrcPath;
delete szNewDstPath;
if(!bIsSucceeded)
{
FindClose(hFindFile);
return FALSE;
}
}
FindClose(hFindFile);
return bIsSucceeded;
}
BOOL DeleteDirectory(LPCTSTR szPath)
{
//修正查找字符串
DWORD dwPathLen = _tcslen(szPath);
LPTSTR szDeletePath = new TCHAR[dwPathLen + 5];
_tcscpy_s(szDeletePath,dwPathLen + 5,szPath);
if(szDeletePath[dwPathLen - 1] != _T('\\'))
_tcscat_s(szDeletePath,dwPathLen + 5,_T("\\"));
_tcscat_s(szDeletePath,dwPathLen + 5,_T("*.*"));
WIN32_FIND_DATA FindData;
HANDLE hFindFile = FindFirstFile(szDeletePath,&FindData);
delete szDeletePath;
if(hFindFile == INVALID_HANDLE_VALUE)return FALSE;
BOOL bIsSucceeded;
while(FindNextFile(hFindFile,&FindData))
{
if(FindData.cFileName[0] == _T('.'))continue;
//生成路径字符串
DWORD dwFileLen = _tcslen(FindData.cFileName);
szDeletePath = new TCHAR[dwPathLen + dwFileLen + 2];
_tcscpy_s(szDeletePath,dwPathLen + dwFileLen + 2,szPath);
if(szDeletePath[dwPathLen - 1] != _T('\\'))
_tcscat_s(szDeletePath,dwPathLen + dwFileLen + 2,_T("\\"));
_tcscat_s(szDeletePath,dwPathLen + dwFileLen + 2,FindData.cFileName);
if(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
bIsSucceeded = DeleteDirectory(szDeletePath);
}
else
{
bIsSucceeded = DeleteFile(szDeletePath);
}
delete szDeletePath;
if(!bIsSucceeded)
{
FindClose(hFindFile);
return FALSE;
}
}
FindClose(hFindFile);
//生成要删除的文件夹路径字符串
szDeletePath = new TCHAR[dwPathLen + 2];
_tcscpy_s(szDeletePath,dwPathLen + 2,szPath);
if(szDeletePath[dwPathLen - 1] == _T('\\'))
szDeletePath[dwPathLen - 1] = _T('\0');
//删除文件夹
bIsSucceeded = RemoveDirectory(szDeletePath);
delete szDeletePath;
return bIsSucceeded;
}
BOOL MoveDirectory(LPCTSTR szSourcePath,LPCTSTR szDestPath)
{
return CopyDirectory(szSourcePath,szDestPath) && DeleteDirectory(szSourcePath);
}
复制代码
欢迎光临 SKY外语计算机学习 (http://join.skywj.com/)
Powered by Discuz! X2.5