ディレクトリ(フォルダ)をコピーする

実装方法としては、
・ディレクトリの中のファイルをコピーする処理を作成する方法
・シェル関数を用いる方法
の2つの実現実装方法があります。

API版

BOOL CopyDirectory( LPCTSTR lpExistingDirectoryName,
					LPCTSTR lpNewDirectoryName )
{
	// 入力値チェック
	if( NULL == lpExistingDirectoryName
	 || NULL == lpNewDirectoryName )
	{
		return FALSE;
	}

	// ディレクトリ名の保存(終端に'\'がないなら付ける)
	TCHAR szDirectoryPathName_existing[_MAX_PATH];
	_tcsncpy_s( szDirectoryPathName_existing, _MAX_PATH, lpExistingDirectoryName, _TRUNCATE );
	if( '\\' != szDirectoryPathName_existing[_tcslen(szDirectoryPathName_existing) - 1] )
	{	// 一番最後に'\'がないなら付加する。
		_tcsncat_s( szDirectoryPathName_existing, _MAX_PATH, _T("\\"), _TRUNCATE );
	}
	TCHAR szDirectoryPathName_new[_MAX_PATH];
	_tcsncpy_s( szDirectoryPathName_new, _MAX_PATH, lpNewDirectoryName, _TRUNCATE );
	if( '\\' != szDirectoryPathName_new[_tcslen(szDirectoryPathName_new) - 1] )
	{	// 一番最後に'\'がないなら付加する。
		_tcsncat_s( szDirectoryPathName_new, _MAX_PATH, _T("\\"), _TRUNCATE );
	}

	if( -1 == _taccess( szDirectoryPathName_existing, 0 ) )
	{
		return FALSE;
	}
	if( 0 == _tcsicmp( szDirectoryPathName_existing, szDirectoryPathName_new ) )
	{
		return FALSE;
	}

	// 新たなディレクトリの作成
	CreateDirectory( szDirectoryPathName_new, NULL );

	// ディレクトリ内のファイル走査用のファイル名作成
	TCHAR szFindFilePathName[_MAX_PATH];
	_tcsncpy_s( szFindFilePathName, _MAX_PATH, szDirectoryPathName_existing, _TRUNCATE );
	_tcsncat_s( szFindFilePathName, _T("*"), _TRUNCATE );

	// ディレクトリ内のファイル走査開始
	WIN32_FIND_DATA		fd;
	HANDLE hFind = FindFirstFile( szFindFilePathName, &fd );
	if( INVALID_HANDLE_VALUE == hFind )
	{	// 走査対象フォルダが存在しない。
		return FALSE;
	}

	do
	{
		if( '.' != fd.cFileName[0] )
		{
			TCHAR szFoundFilePathName_existing[_MAX_PATH];
			_tcsncpy_s( szFoundFilePathName_existing, _MAX_PATH, szDirectoryPathName_existing, _TRUNCATE );
			_tcsncat_s( szFoundFilePathName_existing, _MAX_PATH, fd.cFileName, _TRUNCATE );

			TCHAR szFoundFilePathName_new[_MAX_PATH];
			_tcsncpy_s( szFoundFilePathName_new, _MAX_PATH, szDirectoryPathName_new, _TRUNCATE );
			_tcsncat_s( szFoundFilePathName_new, _MAX_PATH, fd.cFileName, _TRUNCATE );

			if( FILE_ATTRIBUTE_DIRECTORY & fd.dwFileAttributes )
			{	// ディレクトリなら再起呼び出しでコピー
				if( !CopyDirectory( szFoundFilePathName_existing, szFoundFilePathName_new ) )
				{
					FindClose( hFind );
					return FALSE;
				}
			}
			else
			{	// ファイルならWin32API関数を用いてコピー
				if( !CopyFile( szFoundFilePathName_existing, szFoundFilePathName_new, FALSE ) )
				{
					FindClose( hFind );
					return FALSE;
				}
			}
		}
	} while( FindNextFile( hFind, &fd ) );

	FindClose( hFind );

	return TRUE;
}

シェル版

BOOL CopyDirectoryUseShellFunc( LPCTSTR lpExistingDirectoryName,
								LPCTSTR lpNewDirectoryName )
{
	// 入力値チェック
	if( NULL == lpExistingDirectoryName
	 || NULL == lpNewDirectoryName )
	{
		return FALSE;
	}

	// ディレクトリ名の保存(終端に'\'がないなら付ける)(終端を、"\0\0"にする)
	TCHAR szDirectoryPathName_existing[_MAX_PATH];
	_tcsncpy_s( szDirectoryPathName_existing, _MAX_PATH, lpExistingDirectoryName, _TRUNCATE );
	if( '\\' == szDirectoryPathName_existing[_tcslen(szDirectoryPathName_existing) - 1] )
	{	// 一番最後に'\'があるなら取り去る。
		szDirectoryPathName_existing[_tcslen(szDirectoryPathName_existing) -1] = '\0';
	}
	szDirectoryPathName_existing[_tcslen(szDirectoryPathName_existing) + 1] = '\0';
	TCHAR szDirectoryPathName_new[_MAX_PATH];
	_tcsncpy_s( szDirectoryPathName_new, _MAX_PATH, lpNewDirectoryName, _TRUNCATE );
	if( '\\' == szDirectoryPathName_new[_tcslen(szDirectoryPathName_new) - 1] )
	{	// 一番最後に'\'があるなら取り去る。
		szDirectoryPathName_new[_tcslen(szDirectoryPathName_new) -1] = '\0';
	}
	szDirectoryPathName_new[_tcslen(szDirectoryPathName_new) + 1] = '\0';

	if( -1 == _taccess( szDirectoryPathName_existing, 0 ) )
	{
		return FALSE;
	}
	if( 0 == _tcsicmp( szDirectoryPathName_existing, szDirectoryPathName_new ) )
	{
		return FALSE;
	}

	SHFILEOPSTRUCT fos;
	ZeroMemory( &fos, sizeof(SHFILEOPSTRUCT) );
	fos.hwnd	= NULL;
	fos.wFunc	= FO_COPY;
 	fos.pFrom	= szDirectoryPathName_existing;
 	fos.pTo		= szDirectoryPathName_new;
	fos.fFlags	= FOF_NOCONFIRMATION | FOF_MULTIDESTFILES | FOF_NOERRORUI | FOF_SILENT;	// | FOF_NOCONFIRMMKDIR
	if( SHFileOperation( &fos ) )
	{	// 成功すると0が、失敗すると0以外が返る。
		return FALSE;
	}
	return TRUE;
}

ネタ元