#include "stdafx.h"
#include "deleteTempFile.h"
#include <iostream>
using namespace std;
void deleteTempFile::DeleteFileInDir(TCHAR* rootDir)
{
WIN32_FIND_DATA fd;
ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
HANDLE hSearch;
TCHAR filePathName[MAX_PATH];
TCHAR tmpPath[MAX_PATH];
ZeroMemory(filePathName, MAX_PATH);
ZeroMemory(tmpPath, MAX_PATH);
wcscpy(filePathName, rootDir);
BOOL bSearchFinished = FALSE;
if( filePathName[wcslen(filePathName) -1] != '\\' )
{
wcscat(filePathName, L"\\");
}
wcscat(filePathName, L"*");
hSearch = FindFirstFile(filePathName, &fd);
//Is directory
if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
&& wcscmp(fd.cFileName, L".") && wcscmp(fd.cFileName, L"..") )
{
wcscpy(tmpPath, rootDir);
if( rootDir[wcslen(rootDir) -1] != '\\' )
{
wcscat(tmpPath, L"\\");
}
wcscat(tmpPath, fd.cFileName);
wcout.imbue(locale("chs") );
wcout<<L"Dir "<<tmpPath<<endl;
DeleteFileInDir(tmpPath);
}
else if( wcscmp(fd.cFileName, L".") && wcscmp(fd.cFileName, L"..") )
{
FILETIME ftCreationTime=fd.ftCreationTime;
SYSTEMTIME localTime;
SYSTEMTIME filecreTime;
FileTimeToSystemTime(
&ftCreationTime,
&filecreTime);
GetLocalTime(&localTime);
if(IsDeleteEnable(localTime.wYear,localTime.wMonth,localTime.wDay,filecreTime.wYear,filecreTime.wMonth,filecreTime.wDay))
{
wcscpy(tmpPath, rootDir);
if( rootDir[wcslen(rootDir) -1] != '\\' )
{
wcscat(tmpPath, L"\\");
}
wcscat(tmpPath, fd.cFileName);
SetFileAttributes(tmpPath,FILE_ATTRIBUTE_NORMAL);
DeleteFile(tmpPath);
wcout.imbue(locale("chs") );
wcout<<L"fileName "<<fd.cFileName<<L"delete"<<endl;
}
wcout.imbue(locale("chs") );
wcout<<L"fileName "<<fd.cFileName<<endl;
}
while( !bSearchFinished )
{
if( FindNextFile(hSearch, &fd) )
{
if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
&& wcscmp(fd.cFileName, L".") && wcscmp(fd.cFileName, L"..") )
{
wcscpy(tmpPath, rootDir);
if( rootDir[wcslen(rootDir) -1] != '\\' )
{
wcscat(tmpPath, L"\\");
}
wcscat(tmpPath, fd.cFileName);
wcout.imbue(locale("chs") );
wcout<<L"Dir "<<tmpPath<<endl;
DeleteFileInDir(tmpPath);
}
else if( wcscmp(fd.cFileName, L".") && wcscmp(fd.cFileName, L"..") )
{
FILETIME ftCreationTime=fd.ftCreationTime;
SYSTEMTIME localTime;
SYSTEMTIME filecreTime;
FileTimeToSystemTime(
&ftCreationTime,
&filecreTime);
GetLocalTime(&localTime);
if(IsDeleteEnable(localTime.wYear,localTime.wMonth,localTime.wDay,filecreTime.wYear,filecreTime.wMonth,filecreTime.wDay))
{
wcscpy(tmpPath, rootDir);
if( rootDir[wcslen(rootDir) -1] != '\\' )
{
wcscat(tmpPath, L"\\");
}
wcscat(tmpPath, fd.cFileName);
SetFileAttributes(tmpPath,FILE_ATTRIBUTE_NORMAL);
DeleteFile(tmpPath);
wcout.imbue(locale("chs") );
wcout<<L"fileName "<<fd.cFileName<<L"delete"<<endl;
}
wcout.imbue(locale("chs"));
wcout<<L"fileName "<<fd.cFileName<<endl;
}
}
else
{
if( GetLastError() == ERROR_NO_MORE_FILES ) //Normal Finished
{
bSearchFinished = TRUE;
}
else
{
DWORD ret;
ret=GetLastError();
cout<<"文件夹非正常退出 错误代码为"<<ret<<endl;
bSearchFinished = TRUE; //Terminate Search
}
}
}
FindClose(hSearch);
}
BOOL deleteTempFile::isLeapYear(int Year)
{
if ( ( Year%4 == 0 &&Year%100 != 0 ) || ( Year%400 == 0 ) )
{
return TRUE;
}
else
{
return FALSE;
}
}
BOOL deleteTempFile::IsDeleteEnable(int cur_year,int cur_month,int cur_day,int file_cre_year,int file_cre_month,int file_cre_day)
{
int g_nDayOfMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int g_nDayOfMonthn[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(cur_year>file_cre_year)
return TRUE;
if((cur_month-file_cre_month)>1)
return TRUE;
if(cur_month==file_cre_month)
{
int day_cur=cur_day;
int day_file=file_cre_day;
if((day_cur-day_file)>7)
return TRUE;
}
if((cur_month-file_cre_month)==1)
{
int day_cur=cur_day;
int day_file=file_cre_day;
if(isLeapYear(cur_year))
{
if((((day_file+7)-g_nDayOfMonthn[file_cre_month])>0&&((day_file+7)-g_nDayOfMonthn[file_cre_month])<day_cur))
return TRUE;
if(((day_file+7)-g_nDayOfMonthn[file_cre_month])<=0)
return TRUE;
}
else
{
if((((day_file+7)-g_nDayOfMonth[file_cre_month])>0&&((day_file+7)-g_nDayOfMonth[file_cre_month])<day_cur))
return TRUE;
if(((day_file+7)-g_nDayOfMonth[file_cre_month])<=0)
return TRUE;
}
}
return FALSE;
}