⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 common.cpp

📁 Windows环境下不同文件夹下文件的比较、合并
💻 CPP
字号:
/*********************************************************************
File Name		:	Common.cpp
Discription		:	Define common function

Author			:	Chenel
Date			:	2005/10/22
*********************************************************************/

/*********************************************************************

	Include section

*********************************************************************/
#include "StdAfx.h"
#include <io.h>
#include "Common.h"

/*********************************************************************

	Global varible declare section

*********************************************************************/

ErrorFileInfo_t		gstFileInfo;

/*********************************************************************

	Func declare section

*********************************************************************/
/*********************************************************************
Function Name	:	InitialErrorFile
Discription		:	Initial error file

Author			:	Chenel
Date			:	2005/10/23
*********************************************************************/
int InitialErrorFile( CString& strFolderName )
{
	gstFileInfo.strFolderName = strFolderName;
	gstFileInfo.ErrorTable.RemoveAll();

	return SUCCESS;
}
/*********************************************************************
Function Name	:	AddErrInfo
Discription		:	Add error information

Author			:	Chenel
Date			:	2005/10/23
*********************************************************************/
int AddErrInfo( CString strErrType, CString& strErrContent )
{
	CString		strWholeContent = NULSTRING;

	strWholeContent = strErrType + ERRCONBREAK + strErrContent;
	gstFileInfo.ErrorTable.Add( strWholeContent );
	return SUCCESS;
}
/*********************************************************************
Function Name	:	WriteErrFile
Discription		:	Write error file

Author			:	Chenel
Date			:	2005/10/23
*********************************************************************/
int WriteErrFile()
{
	int		iRet = 0;
	int		iCount = 0;
	int		iLoop = 0;
	CString	strFileName = NULSTRING;
	CString	strBreak = _T("\r\n");
	CString	strErrorContent = NULSTRING;

	iCount = gstFileInfo.ErrorTable.GetSize();
	if(0 == iCount)
	{
		return SUCCESS;
	}

	// Open file
	strFileName = gstFileInfo.strFolderName + FILEPATHFLG + ERRFILENAME;
	FILE*	pFile = NULL;

	pFile = _tfopen(strFileName, _T("wb"));
	if( NULL == pFile )
	{
		ASSERT( FALSE );
		return FAILURE;
	}

	// Write file
	for( iLoop = 0; iLoop < iCount; iLoop++ )
	{
		strErrorContent += strBreak;
		strErrorContent += gstFileInfo.ErrorTable.GetAt(iLoop);
	}
	int iLen = strErrorContent.GetLength();
	fwrite(strErrorContent.GetBuffer(0), iLen * sizeof(TCHAR), 1, pFile);

	// Close file
	fclose( pFile );


	return SUCCESS;
}
/*********************************************************************
Function Name	:	SourceFolderReasonable
Discription		:	Judge whether source folder is reasonable
Return value	:	TRUE/FALSE

Author			:	Chenel
Date			:	2005/10/22
*********************************************************************/
BOOL SourceFolderIsReasonable( CString& strFolerName )
{
	if(0 != _taccess(strFolerName,0))
	{
		return FALSE;
	}

	return TRUE;
}
/*********************************************************************
Function Name	:	DestFolderIsReasonable
Discription		:	Judge whether dest folder is reasonable
Return value	:	TRUE/FALSE

Author			:	Chenel
Date			:	2005/10/22
*********************************************************************/
BOOL DestFolderIsReasonable( CString& strFolerName )
{
	if(0 != _taccess(strFolerName,0))
	{
		return FALSE;
	}

	return TRUE;
}
/*********************************************************************
Function Name	:	MakeDirectory
Discription		:	Create specified directory
Return value	:	SUCCESS/FAILURE

Author			:	Chenel
Date			:	2005/10/22
*********************************************************************/
int MakeDirectory( CString& strDirectory )
{
	return SUCCESS;
}
/*********************************************************************
Function Name	:	GetSpecifyFolderName
Discription		:	Get folder name by user specified
Return value	:	SUCCESS/FAILURE

Author			:	Chenel
Date			:	2005/10/22
*********************************************************************/
int GetSpecifyFolderName( HWND hDlgHandle, CString& strFolerName )
{
	ITEMIDLIST*			pItemIdList;
	BROWSEINFO			BrowseInfo;
	TCHAR				stzFolder[_MAX_PATH];  
 
	memset(stzFolder, 0, _MAX_PATH*sizeof(TCHAR));
	BrowseInfo.hwndOwner = hDlgHandle;
	BrowseInfo.pidlRoot = NULL;
	BrowseInfo.pszDisplayName = stzFolder;
	BrowseInfo.lpszTitle = _T("Select folder");
	BrowseInfo.ulFlags = BIF_RETURNONLYFSDIRS|BIF_EDITBOX;
	BrowseInfo.lpfn = NULL;
 
	pItemIdList = SHBrowseForFolder(&BrowseInfo);
	SHGetPathFromIDList(pItemIdList, stzFolder);
	strFolerName = stzFolder;

	return SUCCESS;
}
/*********************************************************************
Function Name	:	GetAllSubFilesIntoOneFolder
Discription		:	Get all sub files into one folder
Return value	:	SUCCESS/FAILURE

Author			:	Chenel
Date			:	2005/10/22
*********************************************************************/
int GetAllSubFilesIntoOneFolder( CString BaseFolderDir, 
			CString DestFolderDir, CStringArray& BaseFilePathTable )
{
	BOOL				bRet = FALSE;
	int					iRet = 0;
	int					iFileCount = 0;
	int					iLoop = 0;
	CString				strOriginalFilePath;
	CString				strDestFilePath;
	CString				strFileName;


	// Copy files
	iFileCount = BaseFilePathTable.GetSize();
	for( iLoop = 0; iLoop < iFileCount; iLoop++ )
	{
		strOriginalFilePath = BaseFilePathTable.GetAt( iLoop );
		CFile File( strOriginalFilePath, CFile::modeRead );
		strFileName = File.GetFileName();
		File.Close();
		strDestFilePath = DestFolderDir + FILEPATHFLG + strFileName;

		if( 0 == _taccess(strDestFilePath,0) )
		{
			// Check whether two files are identical
			BOOL bIdenticalFlg = TwoFilesAreIdentical(strOriginalFilePath, strDestFilePath);

			if(TRUE != bIdenticalFlg)
			{
				CString		strWarnning = NULSTRING;

				strWarnning = _T("Exist a same name file   but contents different <");
				strWarnning += strFileName;
				strWarnning += _T(">   So have not copied <");
				strWarnning += strOriginalFilePath;
				strWarnning += _T("> into dest folder.");
				AddErrInfo( ERRTYPE_WARNNING, strWarnning );
			}

			continue;
		}

		bRet = CopyFile( strOriginalFilePath, strDestFilePath, FALSE );
		if( FALSE == bRet )
		{
			CString		strError = NULSTRING;
			strError = _T("Copy file <");
			strError += strOriginalFilePath;
			strError += _T("> failure!");
			AddErrInfo( ERRTYPE_ERR, strError );
			continue;
		}
	}

	return SUCCESS;
}
/*********************************************************************
Function Name	:	GetAllSubFilesPath
Discription		:	Get all sub file path
Return value	:	SUCCESS/FAILURE

Author			:	Chenel
Date			:	2005/10/22
*********************************************************************/
int GetAllSubFilesPath( CString strDir, CStringArray& FileList )
{
	BOOL		bSearchResult = FALSE;
	CString		strSearchRange;
	CString		strFilePath;
	CFileFind	finder;

	strSearchRange = strDir + FILEFINDTYPE_ALL;
	bSearchResult = finder.FindFile( strSearchRange );
	while( bSearchResult )
	{
		// Get next file
		bSearchResult = finder.FindNextFile();
		if( (finder.IsDots()) || (finder.IsSystem()) || (finder.IsTemporary()) )
		{
			continue;
		}
			
		if (finder.IsDirectory())
		{
			CString strSubDir = finder.GetFilePath();
			GetAllSubFilesPath( strSubDir, FileList );
			continue;
		}

		strFilePath = finder.GetFilePath();
		FileList.Add( strFilePath);
	}

	finder.Close();
	return SUCCESS;
}
/*********************************************************************
Function Name	:	DelFolderCounterFiles
Discription		:	Delete counter folder file
Return value	:	SUCCESS/FAILURE

Author			:	Chenel
Date			:	2005/10/22
*********************************************************************/
int DelFolderCounterFiles( CString BaseFolderDir, CString DestFolderDir, 
			CStringArray& BaseFilePathTable, CStringArray& DestFilePathTable, 
			int iDelType )
{
	BOOL				bRet = FALSE;
	BOOL				bExistSameFileFlg = FALSE;
	int					iBaseCount = 0;
	int					iDestCount = 0;
	int					iBaseLoop = 0;
	int					iDestLoop = 0;
	int					iBaseFolderLen = 0;
	int					iDestFolderLen = 0;
	CString				strBaseSubPath = NULSTRING;
	CString				strDestSubPath = NULSTRING;
	CString				strTempPath = NULSTRING;
	CStringArray		BaseSubTable;
	CStringArray		DestSubTable;

	// Get base folder's sub file path
	iBaseFolderLen = BaseFolderDir.GetLength();
	iBaseCount = BaseFilePathTable.GetSize();
	for( iBaseLoop = 0; iBaseLoop < iBaseCount; iBaseLoop++ )
	{
		strTempPath = BaseFilePathTable.GetAt(iBaseLoop);
		strBaseSubPath = strTempPath.Right(strTempPath.GetLength() - iBaseFolderLen );
		BaseSubTable.Add( strBaseSubPath );
	}

	// Get dest folder's sub file path
	iDestFolderLen = DestFolderDir.GetLength();
	iDestCount = DestFilePathTable.GetSize();
	for( iDestLoop = 0; iDestLoop < iDestCount; iDestLoop++ )
	{
		strTempPath = DestFilePathTable.GetAt(iDestLoop);
		strDestSubPath = strTempPath.Right(strTempPath.GetLength() - iDestFolderLen );
		DestSubTable.Add( strDestSubPath );
	}

	// Start delete
	for( iDestLoop = 0; iDestLoop < iDestCount; iDestLoop++ )
	{
		strTempPath = DestFilePathTable.GetAt( iDestLoop );
		strDestSubPath = DestSubTable.GetAt( iDestLoop );
		for( iBaseLoop = 0; iBaseLoop < iBaseCount; iBaseLoop++ )
		{
			strBaseSubPath = BaseSubTable.GetAt( iBaseLoop );
			if( strBaseSubPath == strDestSubPath )
			{
				break;
			}
		}
		if( iBaseLoop < iBaseCount )
		{
			bExistSameFileFlg = TRUE;
		}
		else
		{
			bExistSameFileFlg = FALSE;
		}

		if( ( !bExistSameFileFlg && (OPTYPE_DELDIFFERENTFILE == iDelType) ) ||
			( bExistSameFileFlg && (OPTYPE_DELSAMEFILE == iDelType) ) )
		{
			SetFileAttributes( strTempPath, FILE_ATTRIBUTE_NORMAL );
			bRet = DeleteFile( strTempPath );
			if( FALSE == bRet )
			{
				CString		strError = NULSTRING;
				strError = _T("Delete file <");
				strError += strTempPath;
				strError += _T("> failure!");
				AddErrInfo( ERRTYPE_ERR, strError );
				continue;
			}
		}
	}


	return SUCCESS;
}

/*********************************************************************
Function Name	:	TwoFilesAreIdentical
Discription		:	Whether two files are identical
Return value	:	TRUE/FALSE

Author			:	Chenel
Date			:	2006/06/28
*********************************************************************/
int TwoFilesAreIdentical(CString strFile1, CString strFile2)
{
	BOOL bRet = FALSE;
	int iOriFileLen = 0;
	int iDestFileLen = 0;

	// Open two files
	CFile File1;
	CFile File2;

	if((0 == File1.Open(strFile1, CFile::modeRead)) || 
		(0 == File2.Open(strFile2, CFile::modeRead)))
	{
		ASSERT(FALSE);
		return FALSE;
	}

	// Get two files's size
	unsigned long iFile1Size = File1.GetLength();
	unsigned long iFile2Size = File2.GetLength();
	if(iFile1Size != iFile2Size)
	{
		return FALSE;
	}

	// Check every block of two files
	void* File1Block = NULL;
	void* File2Block = NULL;
	File1Block = malloc(iFile1Size);
	if(NULL == File1Block)
	{
		ASSERT(FALSE);
		return FALSE;
	}
	memset(File1Block, 0, sizeof(File1Block));
	File2Block = malloc(iFile2Size);
	if(NULL == File2Block)
	{
		ASSERT(FALSE);
		free(File1Block);
		return FALSE;
	}
	memset(File2Block, 0, sizeof(File2Block));

	if(iFile1Size != File1.ReadHuge(File1Block, iFile1Size))
	{
		ASSERT(FALSE);
		free(File1Block);
		free(File2Block);
		return FALSE;
	}
	if(iFile2Size != File2.ReadHuge(File2Block, iFile2Size))
	{
		ASSERT(FALSE);
		free(File1Block);
		free(File2Block);
		return FALSE;
	}

	if(0 == memcmp(File1Block, File2Block, iFile1Size))
	{
		bRet = TRUE;
	}

	free(File1Block);
	free(File2Block);

	return bRet;
}
// End of file

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -