1. 程式人生 > >C++複製檔案

C++複製檔案

這裡我將複製檔案操作封裝成一個介面,具體函式可以去查閱MSDN,相信你看過我這篇部落格後,基本上關於檔案的刪除,拷貝,移動,改名都能順利完成,因為他們用的是同一個函式:)

我這個介面不需要傳具體的檔名字,只需傳存放該檔案的目錄即可,當然你也可以通過該部落格改寫介面

先定義兩個返回值型別

#define TRUE 0
#define FALSE 1

 

int CopyORBackUpFiles(string strFromPath,string strToPath)
{
	//判斷一個路徑下資料夾是否存在
	BOOL rec = PathFileExistsA(strFromPath.c_str());
	if (0 ==rec)
	{
		return FALSE;
	}

	//查詢檔案是否存在
	strFromPath+="\\";
	string strFilesPath = strFromPath;
	strFromPath+="*.*";

	//替換檔案路徑
	strToPath+="\\";

	WIN32_FIND_DATA longFile;//定義檔案資訊結構體

	HANDLE HFileHandle = FindFirstFileA(strFromPath.c_str(),(LPWIN32_FIND_DATAA)&longFile);
	if (INVALID_HANDLE_VALUE == HFileHandle)
	{
		cout << GetLastError() << endl;
	}
	do 
	{
		if(strcmp((char *)longFile.cFileName,".") == 0 || strcmp((char *)longFile.cFileName,"..") == 0)
		{
			continue;
		}

		//wcout << longFile_1.cFileName << endl;
		//WIN32_FIND_DATA結構體中為Wchar,轉為char
		int len = wcslen(longFile.cFileName);
		int ichar = WideCharToMultiByte(CP_ACP,0,longFile.cFileName,len,NULL,0,NULL,NULL);
		char * pBuffer = new char[ichar + 1];
		if (pBuffer)
		{
			ZeroMemory(pBuffer,ichar+1);
		}
		WideCharToMultiByte(CP_ACP,0,longFile.cFileName,len,pBuffer,ichar,NULL,NULL);
		string strTargetFilename;
		for (int i=0; i<ichar; i++)
		{
			strTargetFilename += pBuffer[i];
		}
		delete pBuffer;
		pBuffer = NULL;

		string ZIPFileName = strFilesPath;

		ZIPFileName+=strTargetFilename;
		//將解壓檔案路徑轉為TCHAR型別
		int iUnicode = MultiByteToWideChar(CP_ACP, 0, ZIPFileName.c_str(), ZIPFileName.length(), NULL, 0);
		WCHAR* pwUnicode = new WCHAR[iUnicode + 2];
		if (pwUnicode)
		{
			ZeroMemory(pwUnicode, iUnicode + 2);
		}
		MultiByteToWideChar(CP_ACP, 0, ZIPFileName.c_str(), ZIPFileName.length(), pwUnicode, iUnicode);
		pwUnicode[iUnicode] = '\0';
		pwUnicode[iUnicode+1] = '\0';
		int iRelativePath = MultiByteToWideChar(CP_ACP, 0, strToPath.c_str(), strToPath.length(), NULL, 0);
		WCHAR* pwRelativePath = new WCHAR[iRelativePath + 2];
		if (pwRelativePath)
		{
			ZeroMemory(pwRelativePath, iRelativePath + 2);
		}
		MultiByteToWideChar(CP_ACP, 0, strToPath.c_str(), strToPath.length(), pwRelativePath, iRelativePath);
		pwRelativePath[iRelativePath] = '\0';
		pwRelativePath[iRelativePath+1] = '\0';
		

		//拷貝替換檔案
		SHFILEOPSTRUCT FileOp;                   //填寫SHFILEOPSTRUCT結構體,通過SHFileOperation告訴windows該做什麼

		FileOp.hwnd = NULL;
		FileOp.wFunc = FO_COPY;
		FileOp.pFrom = (LPCWSTR)pwUnicode;       //路徑必須以雙NULL結尾   ,原始檔路徑
		FileOp.pTo = pwRelativePath;             //路徑必須以雙NULL結尾   ,目的檔案路徑
		FileOp.fFlags = FOF_NOCONFIRMATION | FOF_SILENT;
		FileOp.hNameMappings = NULL;
		FileOp.lpszProgressTitle = NULL;

		//可以實現各種檔案操作,例如檔案的拷貝,移動,刪除等
		int nRet = SHFileOperation(&FileOp);

		delete pwUnicode;
		pwUnicode = NULL;

		delete pwRelativePath;
		pwRelativePath = NULL;

		if(0 != nRet)
		{
			return FALSE;
		}

	} while (FindNextFile(HFileHandle,&longFile) != 0); //判斷當前目錄下有沒有下一目錄或檔案
	FindClose(HFileHandle);  //關閉由FindFirstFile開啟的搜尋控制代碼
}

如果你編譯出錯,那麼可能是某個函式沒有新增標頭檔案或引用庫

可以新增這句試試

#include <Shlwapi.h> 

#include <shlobj.h>

#pragma comment(lib, "shlwapi.lib")