Ejemplo n.º 1
0
/**************************************************************************
 * function CopyFiles
 *
 * written by moonknit
 *
 * @history
 * created 2006-02-22
  *
 * @Description
 * 복수의 파일의 복사를 수행한다.
 *
 * @Parameters
 * (in CString) a - 소스가 되는 파일, *.* 등 허용
 * (in CString) b - 대상이 되는 디렉토리 또는 파일명 (한개의 파일을 복사할 때는 파일명을 넣는다.)
 * (in BOOL) bmove - 파일을 옮긴다.
 **************************************************************************/
BOOL CopyFiles(CString a, CString b, BOOL bmove)
{
	CFileFind f;
	BOOL bloop;
	BOOL bdestdir = TRUE;
	BOOL bsourcesingle = FALSE;

	TRACE(_T("copy [ %s ] to [ %s ]\r\n"), a, b);

	if(a.GetLength() == 0 || b.GetLength() == 0) return FALSE;

	bloop = f.FindFile(b);
	if(bloop)
	{
		bdestdir = FALSE;
	}

	if(bdestdir && b.Right(1) != _T("\\")) b+= _T("\\");

	f.Close();

	if(a.Find(_T("*")) == -1)
		bsourcesingle = TRUE;

	if((bsourcesingle && bdestdir)
		|| (!bsourcesingle && !bdestdir)
		) return FALSE;

	bloop = f.FindFile(a);

	if(!bloop) return FALSE;

	if(bsourcesingle)
	{
		while(bloop)
		{
			bloop = f.FindNextFile();

			if(f.IsNormal())
			{
				if(bmove)
					return MoveFile(f.GetFilePath(), b);

				return CopyFile(f.GetFilePath(), b, TRUE);
			}
		}
	}
	else
	{
		CString dest;
		while(bloop)
		{
			bloop = f.FindNextFile();

			if(!f.IsDots() && !f.IsDirectory())
			{
				dest.Format(_T("%s%s"), b, f.GetFileName());
				
//				TRACE(_T("copy file from : %s - to : %s\r\n"), f.GetFilePath(), dest);

				if(bmove)
				{
					if(!MoveFile(f.GetFilePath(), dest))
						return FALSE;
				}
				else
				{
					if(!CopyFile(f.GetFilePath(), dest, FALSE))
						return FALSE;
				}
			}
		}
	}

	return TRUE;
}