www.pudn.com > AutoDispose2.rar > AutoDispose2Dlg.cpp


// AutoDispose2Dlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "AutoDispose2.h" 
#include "AutoDispose2Dlg.h" 
#include  
#include  
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CAutoDispose2Dlg dialog 
 
CAutoDispose2Dlg::CAutoDispose2Dlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CAutoDispose2Dlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CAutoDispose2Dlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
 
 
 
	m_pstFileInfoHead=NULL; 
	m_ulDisPos=1; 
	m_blAppRunFlag=TRUE; 
} 
 
void CAutoDispose2Dlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAutoDispose2Dlg) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAutoDispose2Dlg, CDialog) 
	//{{AFX_MSG_MAP(CAutoDispose2Dlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_AUTO_START, OnAutoStart) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CAutoDispose2Dlg message handlers 
 
BOOL CAutoDispose2Dlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CAutoDispose2Dlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CAutoDispose2Dlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CAutoDispose2Dlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CAutoDispose2Dlg::BrowseDir(CString strDir, UINT uiCallFlag) 
{ 
	CFileFind ff; 
	CString szDir = strDir; 
	 
	if(szDir.Right(1) != "\\") 
		szDir += "\\"; 
	 
	szDir += "*.*"; 
	 
	BOOL boolRes = ff.FindFile(szDir); 
	while( boolRes ) 
	{ 
		if (!boolRes) 
		{ 
			break; 
		} 
		boolRes = ff.FindNextFile(); 
		CString cstrFilename=ff.GetFileName(); 
		//	AfxMessageBox(cstrFilename); 
		 
		SYSTEMTIME CurTime;                   
		GetLocalTime(&CurTime);  
		CString strYear; 
		strYear.Format("%d",CurTime.wYear); 
		while (-1!=cstrFilename.Find(strYear) && ff.IsDirectory()) 
		{ 
			//boolRes=ff.FindNextFile(); 
			break; 
			//cstrFilename=ff.GetFileName(); 
		} 
		if(-1!=cstrFilename.Find(AfxGetApp()->m_pszExeName) && !ff.IsDirectory()) 
		{ 
			//boolRes=ff.FindNextFile(); 
			continue; 
			//cstrFilename=ff.GetFileName(); 
		} 
		if(ff.IsDirectory() && !ff.IsDots()) 
		{ 
			CString strPath = ff.GetFilePath(); 
			BrowseDir( strPath,NULL);  
		} 
		else if(!ff.IsDirectory() && !ff.IsDots()) 
		{ 
			tagFileInfo* pstFileInfo =new tagFileInfo(); 
			pstFileInfo->pszFileName=ff.GetFileName(); 
			pstFileInfo->pszFilePath=ff.GetFilePath(); 
			pstFileInfo->pszFileSize=ff.GetLength(); 
			pstFileInfo->bDisposed=FALSE; 
			pstFileInfo->pNext=NULL; 
			pstFileInfo->pPrev=NULL; 
			pstFileInfo->ulCount=NULL; 
			pstFileInfo->ulDisPos=NULL; 
			AddToDll(pstFileInfo); 
		}	 
	} 
	ff.Close(); 
 
} 
 
void CAutoDispose2Dlg::DEBUG_WriteLog(CString strlogLine, CString strLognane) 
{ 
#if 0 
	HANDLE hFile; 
	hFile=::CreateFile("c:\\moveLog.txt",GENERIC_WRITE,0,NULL, 
		TRUNCATE_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); 
	CFile cfilelog((int)hFile); 
	cfilelog.Write(strlogLine+"\r\n",strlogLine.GetLength()+2); 
	cfilelog.Close(); 
	CloseHandle(hFile); 
#endif 
	 
	FILE *pFile=fopen( strLognane, "a"); 
	fwrite( strlogLine+"\r\n", sizeof(char),strlogLine.GetLength()+2, pFile ); 
	fflush(pFile); 
	fclose(pFile); 
} 
 
void CAutoDispose2Dlg::AddToDll(tagFileInfo * psztempFileInfo) 
{ 
	if (NULL==m_pstFileInfoHead) 
	{ 
		psztempFileInfo->pNext=m_pstFileInfoHead; 
		psztempFileInfo->pPrev=m_pstFileInfoHead; 
	} else { 
		psztempFileInfo->pPrev=m_pstFileInfoHead; 
		psztempFileInfo->pNext=NULL; 
		m_pstFileInfoHead->pNext=psztempFileInfo; 
	} 
	m_pstFileInfoHead=psztempFileInfo; 
} 
void CAutoDispose2Dlg::MovetoFirst() 
{ 
	while (m_pstFileInfoHead->pPrev!=NULL) 
	{ 
		m_pstFileInfoHead=m_pstFileInfoHead->pPrev; 
	} 
} 
void CAutoDispose2Dlg::OnAutoStart()  
{ 
	int size=sizeof(tagFileInfo); 
	if (!m_blAppRunFlag) 
	{	 
		return; 
	} 
	m_blAppRunFlag=FALSE; 
 
	CFileFind ff;	 
	ff.FindFile(); 
	ff.FindNextFile(); 
	 
	BrowseDir(ff.GetFilePath(),NULL); 
	ff.Close();	 
	DisposeFile(); 
	MoveFiles(); 
} 
 
void CAutoDispose2Dlg::DisposeFile() 
{ 
	if (NULL==m_pstFileInfoHead) 
	{ 
		return; 
	} 
	MovetoFirst(); 
	double dbSize=0; 
	BOOL blRunFlag=TRUE; 
	while (TRUE) 
	{ 
		if (!blRunFlag) 
		{ 
			break; 
		} 
		while (TRUE) 
		{ 
			if (TRUE==m_pstFileInfoHead->bDisposed) 
			{ 
				m_pstFileInfoHead=m_pstFileInfoHead->pNext; 
				continue; 
			} 
			dbSize+=m_pstFileInfoHead->pszFileSize; 
			m_pstFileInfoHead->ulDisPos=m_ulDisPos; 
			m_pstFileInfoHead->bDisposed=TRUE; 
			double dbTempSize=dbSize; 
						 
			if (dbSize>RECE_DISPOSE_SIZE && dbSizeMAX_DISPOSE_SIZE) 
			{ 
				m_pstFileInfoHead->ulDisPos=NULL; 
				m_pstFileInfoHead->bDisposed=FALSE;		 
				dbSize=dbSize-(m_pstFileInfoHead->pszFileSize); 
				//jump to next and find the receivability 
			} 
			if (NULL==m_pstFileInfoHead->pNext) 
			{ 
				//if the last one.and the sum size pNext; 
		} 
		m_ulDisPos++; 
		dbSize=0; 
		MovetoFirst(); 
	} 
} 
 
void CAutoDispose2Dlg::MoveFiles() 
{ 
	if (NULL==m_pstFileInfoHead) 
	{ 
		return; 
	} 
	CString strDirFlagEx; 
	for (ULONG i=0;i<=m_ulDisPos;i++) 
	{ 
		MovetoFirst(); 
		while (TRUE) 
		{ 
			if (m_pstFileInfoHead->ulDisPos==i) 
			{ 
				if (0==i || i==m_ulDisPos) 
				{ 
					strDirFlagEx="unDispos"; 
				} else { 
					strDirFlagEx.Format("%d",i); 
				} 
				MoveFilesByOriPath(m_pstFileInfoHead->pszFilePath,strDirFlagEx); 
			} 
			if (NULL==m_pstFileInfoHead->pNext) 
			{ 
				break; 
			} 
			m_pstFileInfoHead=m_pstFileInfoHead->pNext; 
		} 
	} 
} 
 
BOOL CAutoDispose2Dlg::mkdirEx(const char *lpPath) 
{ 
	CString pathname = lpPath; 
	if(pathname.Right(1) != "\\") 
		pathname += "\\" ; 
	int end = pathname.ReverseFind('\\'); 
	int pt = pathname.Find('\\'); 
	if (pathname[pt-1] == ':') 
		pt = pathname.Find('\\', pt+1); 
	CString path; 
	while(pt != -1 && pt<=end) 
	{ 
		path = pathname.Left(pt+1); 
		if(_access(path, 0) == -1) 
			_mkdir(path); 
		pt = pathname.Find('\\', pt+1); 
	} 
	return true; 
} 
 
BOOL CAutoDispose2Dlg::MoveFilesByOriPath(CString strOriPath,CString strDirFlagEx) 
{ 
	/*ǰÖñäÁ¿*/ 
	UINT nCount=0; 
	char charCurrDir[100]; 
	BOOL boolCreateDirflag=TRUE; 
	GetCurrentDirectory(sizeof(charCurrDir),charCurrDir); 
	 
	CString sTime,sYear,sMonth,sDay;      
	SYSTEMTIME CurTime;                   
	GetLocalTime(&CurTime);               
	sYear.Format("%d",CurTime.wYear);   
	sMonth.Format("%d",CurTime.wMonth); 
	if (1==sMonth.GetLength()) 
	{ 
		sMonth="0"+sMonth; 
	} 
 
	CString strSign="\\"; 
	sDay.Format("%d",CurTime.wDay); 
	if (1==sDay.GetLength()) 
	{ 
		sDay="0"+sDay; 
	} 
	sDay=sDay+"_"+strDirFlagEx; 
	sTime =  sYear+ sMonth + sDay;   
	 
	CString strDest=charCurrDir+strSign+sTime+strOriPath.Right (strOriPath.GetLength()-strlen(charCurrDir)); 
	CString strCreateDir=strDest.Left(strDest.ReverseFind('\\')+1); 
 
	if(!mkdirEx(strCreateDir)) 
	{ 
		int  a=GetLastError(); 
		CString strtemp; 
		strtemp.Format("%d",a); 
		AfxMessageBox(strtemp); 
	} 
	if(!MoveFileEx(strOriPath,strDest,MOVEFILE_REPLACE_EXISTING)) 
	{ 
		ULONG ulErrorCode= GetLastError(); 
		CString strErrorCode; 
		strErrorCode.Format("Error Code is %d\n",ulErrorCode); 
		DEBUG_WriteLog("natural -"+strErrorCode+strOriPath+"-->"+strDest,"c:\\AutoDispMoveError.txt"); 
		AfxMessageBox(strErrorCode+"When Move files to "+strDest+" failed!"); 
	} 
	return TRUE; 
}