定时删除服务器文件--FTP管理员一定用得着的软件

        近来单位新开了个FTP服务器,各位管理员都知道的,文件多了就得定时删除啊,网上找了一圈,倒是有个几个软件可以用,但是总觉得效果不是很好,时灵时不灵的。只好痛下决心,自己做一个啦,小弟用的依旧是老掉牙的VC++6.0,不过软件能用就行啦。重点是思路。

        主要功能:

1、 定时删除指定路径下的文件,支持修改时间或者创建时间来选择删除条件;

2、 自动保存已删除文件的名称、删除时间;

3、 支持拖拽或者浏览两种方式选取路径;

4、 可以保存路径信息,载入路径信息,省去软件退出之后又要加载之苦;

5、 意外退出,自动保存当前已有的路径信息;

6、 在重启软件的时候会自动载入路径信息。

         直接上代码吧,有点乱,大家凑合着看哈

// deleteDlg.cpp : implementation file

//

#include "stdafx.h"

#include "delete.h"

#include "deleteDlg.h"

#include "SkinH.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

#define WM_SHOWTASK 518

struct threadparam           //子线程参数

{

  CDeleteDlg* pDlg;

};

struct threadparam thread1;

BOOL ISBegin;

UINT Thread(LPVOID pParam);

CDeleteDlg* DelDlg;

#pragma comment(lib,"SkinH.lib")  //加载皮肤

/////////////////////////////////////////////////////////////////////////////

// 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:

         virtualvoid 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()

/////////////////////////////////////////////////////////////////////////////

// CDeleteDlg dialog

CDeleteDlg::CDeleteDlg(CWnd* pParent/*=NULL*/)

         :CDialog(CDeleteDlg::IDD, pParent)

{

         //{{AFX_DATA_INIT(CDeleteDlg)

         m_keepdays= 0;

         m_delTime= 0;

         m_radio1= 0;  

         m_radio3= 0;

         //}}AFX_DATA_INIT

         //Note that LoadIcon does not require a subsequent DestroyIcon in Win32

         m_hIcon= AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

voidCDeleteDlg::DoDataExchange(CDataExchange* pDX)

{

         CDialog::DoDataExchange(pDX);

         //{{AFX_DATA_MAP(CDeleteDlg)

         DDX_Control(pDX,IDC_LIST1, m_pathlist);

         DDX_Text(pDX,IDC_EDIT1, m_keepdays);

         DDX_Text(pDX,IDC_EDIT2, m_delTime);

         DDV_MinMaxUInt(pDX,m_delTime, 0, 23);

         DDX_Radio(pDX,IDC_RADIO1, m_radio1);

         DDX_Radio(pDX,IDC_RADIO3, m_radio3);

         //}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CDeleteDlg, CDialog)

         //{{AFX_MSG_MAP(CDeleteDlg)

         ON_WM_SYSCOMMAND()

         ON_WM_PAINT()

         ON_WM_QUERYDRAGICON()

         ON_BN_CLICKED(IDstart,OnStart)

         ON_BN_CLICKED(IDC_BUTTON1,OnGetPath)

         ON_WM_TIMER()

         ON_BN_CLICKED(IDC_stop,OnStop)

         ON_BN_CLICKED(IDC_BUTTON2,OnDelPath)

         ON_BN_CLICKED(IDC_BUTTON3,OnSavePath)

         ON_BN_CLICKED(IDC_BUTTON4,OnLoadPath)

         ON_WM_DROPFILES()

         ON_BN_CLICKED(IDC_RADIO1,OnRadio1)

         ON_BN_CLICKED(IDC_RADIO2,OnRadio2)

         ON_WM_SIZE()

         ON_WM_DESTROY()

         ON_BN_CLICKED(IDC_RADIO3,OnRadio3)

         ON_MESSAGE(WM_SHOWTASK,OnShowTask)

         ON_BN_CLICKED(IDC_RADIO4,OnRadio4)

         //}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CDeleteDlg message handlers

BOOL CDeleteDlg::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)

         {

                   CStringstrAboutMenu;

                   strAboutMenu.LoadString(IDS_ABOUTBOX);

                   if(!strAboutMenu.IsEmpty())

                   {

                            pSysMenu->AppendMenu(MF_SEPARATOR);

                            pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX, strAboutMenu);

                   }

         }

         //Set the icon for this dialog.  Theframework does this automatically

         //  when the application's main window is not adialog

         SetIcon(m_hIcon,TRUE);                   // Set big icon

         SetIcon(m_hIcon,FALSE);                 // Set small icon

        

         //TODO: Add extra initialization here

         //皮肤

   SkinH_Attach();

         //最小化到托盘

         m_nid.cbSize  = (DWORD)sizeof(NOTIFYICONDATA);

   m_nid.hWnd    = this->m_hWnd;

   m_nid.uID     = IDR_MAINFRAME;

   m_nid.uFlags  = NIF_ICON |NIF_MESSAGE | NIF_TIP ;

   m_nid.uCallbackMessage = WM_SHOWTASK;            // 自定义的消息名称

   m_nid.hIcon   =LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));

   strcpy(m_nid.szTip, "服务器程序");                // 信息提示条为"服务器程序"

    //载入上次的路径

         CStdioFilefile;

         CFileExceptione;

  

         if(file.Open("LastRecord.ini",CFile::modeRead,&e))

         {

                   CStringpath;

                   while(file.ReadString(path))

                   {

                            m_pathlist.AddString(path);

                   }

         }

         else

         {

                   file.Open("LastRecord.ini",CFile::modeCreate|CFile::modeWrite,&e);

         }

   file.Close();

    //保存记录

         chara[100];

         memset(a,32,100);

         a[99]= '\0';

         CStringb,c;

         b.Format("%s",a);

         c= "文件"+b;

         writecontents("C:\\Record.txt",c+"时间");

        

         GetDlgItem(IDC_stop)->EnableWindow(false);

         SetTimer(1,100,NULL);   //定时器

         returnTRUE;  // return TRUE  unless you set the focus to a control

}

void CDeleteDlg::OnSysCommand(UINT nID,LPARAM lParam)

{

         if((nID & 0xFFF0) == IDM_ABOUTBOX)

         {

                   CAboutDlgdlgAbout;

                   dlgAbout.DoModal();

         }

         else

         {

                   CDialog::OnSysCommand(nID,lParam);

         }

}

// If you add a minimize button to yourdialog, you will need the code below

//  todraw the icon.  For MFC applicationsusing the document/view model,

// this is automatically done for you by the framework.

void CDeleteDlg::OnPaint()

{

         if(IsIconic())

         {

                   CPaintDCdc(this); // device context for painting

                   SendMessage(WM_ICONERASEBKGND,(WPARAM) dc.GetSafeHdc(), 0);

                   //Center icon in client rectangle

                   intcxIcon = GetSystemMetrics(SM_CXICON);

                   intcyIcon = GetSystemMetrics(SM_CYICON);

                   CRectrect;

                   GetClientRect(&rect);

                   intx = (rect.Width() - cxIcon + 1) / 2;

                   inty = (rect.Height() - cyIcon + 1) / 2;

                   //Draw the icon

                   dc.DrawIcon(x,y, m_hIcon);

         }

         else

         {

                   CDialog::OnPaint();

         }

}

// The system calls this to obtain the cursorto display while the user drags

//  theminimized window.

HCURSOR CDeleteDlg::OnQueryDragIcon()

{

         return(HCURSOR) m_hIcon;

        

}

void CDeleteDlg::OnStart()

{

         //TODO: Add your control notification handler code here

         UpdateData(true);

         //删除方式为定时删除

         if(m_radio3 == 0)

         {

         ISBegin= true;

         thread1.pDlg= this;

         AfxBeginThread((AFX_THREADPROC)Thread,&thread1,THREAD_PRIORITY_NORMAL,0,NULL);

        

         GetDlgItem(IDC_EDIT1)->EnableWindow(false);

         GetDlgItem(IDC_EDIT2)->EnableWindow(false);

         GetDlgItem(IDstart)->EnableWindow(false);

         GetDlgItem(IDC_BUTTON1)->EnableWindow(false);

         GetDlgItem(IDC_BUTTON2)->EnableWindow(false);

         GetDlgItem(IDC_BUTTON3)->EnableWindow(false);

         GetDlgItem(IDC_BUTTON4)->EnableWindow(false);

         GetDlgItem(IDC_RADIO1)->EnableWindow(false);

         GetDlgItem(IDC_RADIO2)->EnableWindow(false);

         GetDlgItem(IDC_RADIO3)->EnableWindow(false);

         GetDlgItem(IDC_RADIO4)->EnableWindow(false);

         GetDlgItem(IDC_stop)->EnableWindow(true);

         }

         else

                   main();         //手动删除

        

}

void CDeleteDlg::OnGetPath()        //通过浏览获取文件夹路径

{

         //TODO: Add your control notification handler code here

         CStringReturnPath;

         TCHARpath[256];

         BROWSEINFObi;                          //BROWSEINFO结构变量

   bi.hwndOwner = NULL;

         bi.lpfn= NULL;

         bi.lpszTitle= _T("浏览文件夹");

         bi.pidlRoot= NULL;

         bi.pszDisplayName= path;

         bi.ulFlags= BIF_BROWSEINCLUDEFILES;

         LPITEMIDLISTpltmlDlist = SHBrowseForFolder(&bi);

         if(pltmlDlist)

         {

                   if(SHGetPathFromIDList(pltmlDlist,path))

                   {

                            ReturnPath= path;

                   }

         }

         else

         {

                   ReturnPath= "";

         }

         if(ReturnPath.GetLength()==0)

         {

                   AfxMessageBox("空路径");

             return;

         }

         if(GetFileAttributes(ReturnPath)== FILE_ATTRIBUTE_DIRECTORY)

         {

                   m_pathlist.AddString(ReturnPath);

         }

         else

         {

                   AfxMessageBox("该路径不是一个文件夹");

                   return;

         }

                  

}

void CDeleteDlg::OnCancel()   //退出时自动记录路径信息,防止意外退出而丢失路径信息

{

         //TODO: Add extra cleanup here

         CFilerecordfile("LastRecord.ini",CFile::modeWrite|CFile::modeCreate);

         recordfile.Close();

         UpdateData(true);

         intnum = m_pathlist.GetCount();

         for(inti = 0;i<num;i++)

         {

                   CStringntext;

                   m_pathlist.GetText(i,ntext);

                   writecontents(_T("LastRecord.ini"),ntext);

         }

         CDialog::OnCancel();

}

void CDeleteDlg::OnTimer(UINT nIDEvent)   

{

         //TODO: Add your message handler code here and/or call default

         if(nIDEvent == 1)

         {

                   MoveStatic();

         }

         CDialog::OnTimer(nIDEvent);

}

void CDeleteDlg::OnStop()

{

         //TODO: Add your control notification handler code here

//       KillTimer(0);

         ISBegin= false;

         GetDlgItem(IDstart)->EnableWindow(true);

         GetDlgItem(IDC_stop)->EnableWindow(false);

         GetDlgItem(IDC_EDIT1)->EnableWindow(true);

         GetDlgItem(IDC_EDIT2)->EnableWindow(true);

         GetDlgItem(IDC_BUTTON1)->EnableWindow(true);

         GetDlgItem(IDC_BUTTON2)->EnableWindow(true);

         GetDlgItem(IDC_BUTTON3)->EnableWindow(true);

         GetDlgItem(IDC_BUTTON4)->EnableWindow(true);

         GetDlgItem(IDC_RADIO1)->EnableWindow(true);

         GetDlgItem(IDC_RADIO2)->EnableWindow(true);

         GetDlgItem(IDC_RADIO3)->EnableWindow(true);

         GetDlgItem(IDC_RADIO4)->EnableWindow(true);

}

void CDeleteDlg::OnDelPath()   //删除路径

{

         //TODO: Add your control notification handler code here

         intnSel,nCnt;

         nCnt= m_pathlist.GetCount();

         nSel= m_pathlist.GetCurSel();

         if(nSel != LB_ERR)

         {

                   m_pathlist.DeleteString(nSel);

                   if(nSel < nCnt-1)

                   {

                            m_pathlist.SetCurSel(nSel);

                   }

                   else

                   {

                            m_pathlist.SetCurSel(nSel-1);

                   }

         }

}

void CDeleteDlg::MoveStatic()        //移动的文字

{

         CWnd*rectCwnd;

         CRectrect,clientrect,dlgRect;

         //获取对话框客户区域右极限坐标

         intnleft,nright;

         GetClientRect(dlgRect);

         nright= dlgRect.right;

    //获取控件坐标

         inteleft,eright,etop,ebottom,ewidth,eheight;

         rectCwnd= GetDlgItem(IDC_mystatic);

         rectCwnd->GetWindowRect(rect);

         ScreenToClient(rect);

         eleft= rect.left;

         eright= rect.right;

         ewidth= eright - eleft;

         etop= rect.top;

         ebottom= rect.bottom;

         eheight= ebottom - etop;

         if(eright<0)

         {

                   eleft= nright;

                   rectCwnd->MoveWindow(eleft,etop,ewidth,eheight,true);  //movewindow函数中的坐标是客户区域中的坐标!

         }

         else

         {

                   eleft-= 1;

                   rectCwnd->MoveWindow(eleft,etop,ewidth,eheight,true);

         }

         return;

}

UINT Thread(LPVOID pParam)          //定时线程

{

         structthreadparam *m_thread=(threadparam*)pParam;

         DelDlg= (CDeleteDlg*)m_thread->pDlg;

         while(ISBegin)

         {

                   CTimenow;

                   now= CTime::GetCurrentTime();

                   inth = now.GetHour();

                   intmin = now.GetMinute();

                   intsec = now.GetSecond();

                   UINTdeltime = DelDlg->m_delTime;

                   if(h==deltime&&min==0&&sec==0)

                   {

                            DelDlg->main();

                   }       

         }

         return0;

}

void CDeleteDlg::main()    //判断是否达到删除条件

{

         CFileFindfile;

         CStringszpath,path;

         CTimeptime,nowtime;

         LONGldays;

         ldays= m_keepdays;

         intnum;

         BOOLnRet;

         num= m_pathlist.GetCount();

         for(int i=0;i<num;i++)

         {

                   m_pathlist.GetText(i,szpath);

                   path= szpath + "\\*.*";

                  

                   file.FindFile(path,0);

                   nRet= true;

                   while(nRet)

                   {

                            nRet= file.FindNextFile();

                            if(file.IsDots())

                                     continue;

                            if(file.IsDirectory())

                                     continue;

                            CStringfilename;

                            //判断是否过期

                            if(m_radio1==0)

                                     file.GetCreationTime(ptime);

                            else

                                     file.GetLastWriteTime(ptime);

                           

                            nowtime= CTime::GetCurrentTime();

                            if(nowtime>(ptime+CTimeSpan(ldays,0,0,0)))

                            {

                                     //获取文件路径

                                     filename= file.GetFileName();

                                     CFilefile1;

                                     CStringrFile;

                                     rFile= szpath + "\\" + filename;

                                     file1.Remove(rFile);

                                     CTimet;

                                     t= CTime::GetCurrentTime();

                                     CStrings = t.Format("%Y-%B-%d %H:%M:%S");

                                     writecontents("C:\\Record.txt",rFile+"        "+s);

                            }

                           

                   }

                   file.Close();

         }

}

void CDeleteDlg::OnSavePath()    //保存路径

{

         //TODO: Add your control notification handler code here

         UpdateData(true);

         intncount = m_pathlist.GetCount();

        

         CStringntext,savepath;

        

         CStringstrFilter;

   strFilter.Format("Text Files (*txt)");

         CFileDialogsavedlg(false,"txt",NULL,NULL,strFilter,NULL);

         if(savedlg.DoModal() ==IDOK)

         {

                   savepath= savedlg.GetPathName();

                   for(inti = 0;i<ncount;i++)

                   {

                            m_pathlist.GetText(i,ntext);

                            writecontents(savepath,ntext);

                   }

                  

         }

}

void CDeleteDlg::writecontents(CStringsavepath,CString ntext)           //将信息写入记事本

{

         CFilemyfile;    

                  

         myfile.Open(savepath,CFile::modeNoTruncate|CFile::modeCreate|CFile::modeWrite,NULL);

         myfile.SeekToEnd();

         myfile.Write((LPSTR)(LPCSTR)ntext,ntext.GetLength());

         myfile.Write(_T("\r\n"),2);

         myfile.Close();

}

void CDeleteDlg::OnLoadPath()    //载入以前的信息

{

         //TODO: Add your control notification handler code here

         CStringstrFilter;

   strFilter.Format("TXT Files(*.txt)|*.txt");

         CFileDialogloaddlg(true,NULL,NULL,NULL,strFilter,NULL);

         if(loaddlg.DoModal() == IDOK)

         {

                   CStringLoadpath = loaddlg.GetPathName();

       m_pathlist.ResetContent();

                   CStdioFilefile;

                   file.Open(Loadpath,CFile::modeRead,NULL);

                   CStringpath;

                   while(file.ReadString(path))

                   {

                            m_pathlist.AddString(path);

                   }

         }

        

        

}

void CDeleteDlg::OnDropFiles(HDROPhDropInfo)        //拖拽获取路径

{

         //TODO: Add your message handler code here and/or call default

         CStringstr;

         intmax = 256;

         UINTcount;

         count= ::DragQueryFile(hDropInfo,0xFFFFFFFF,NULL,0);

         for(UINTi=0;i<count;i++)

         {

                   ::DragQueryFile(hDropInfo,i,str.GetBuffer(max),max);

                   str.ReleaseBuffer();

                   if(GetFileAttributes(str)== FILE_ATTRIBUTE_DIRECTORY)

                   {

                            m_pathlist.AddString(str);

                   }

                   else

                   {

                            AfxMessageBox("包含无效路径");

                            return;

                   }

         }

         CDialog::OnDropFiles(hDropInfo);

}

void CDeleteDlg::OnRadio1()      //单选按钮

{

         //TODO: Add your control notification handler code here

         m_radio1= 0;

}

void CDeleteDlg::OnRadio2() //单选按钮

{

         //TODO: Add your control notification handler code here

         m_radio1= 1;

}

void CDeleteDlg::OnSize(UINT nType, int cx,int cy)      //最小化到托盘

{

         CDialog::OnSize(nType,cx, cy);

        

         //TODO: Add your message handler code here

         if(nType== SIZE_MINIMIZED) 

   {

         Shell_NotifyIcon(NIM_ADD,&m_nid);                // 在托盘区添加图标

         ShowWindow(SW_HIDE);// 当最小化市,隐藏主窗口             

   }

}

LRESULT CDeleteDlg::OnShowTask(WPARAM wParam,LPARAM lParam)         //托盘消息响应函数

    {

          if(wParam != IDR_MAINFRAME)

                  return 1;

          switch(lParam)

          {

          case WM_RBUTTONUP:                                        // 右键起来时弹出菜单

                  {

                   

                         LPPOINT lpoint = newtagPOINT;

                        ::GetCursorPos(lpoint);                    // 得到鼠标位置

                        CMenu menu;

                        menu.CreatePopupMenu();                    // 声明一个弹出式菜单

                        menu.AppendMenu(MF_STRING, WM_DESTROY, "关闭");

                                                         SetForegroundWindow();

                        menu.TrackPopupMenu(TPM_LEFTALIGN, lpoint->x ,lpoint->y, this);

                         HMENU hmenu =menu.Detach();

                         menu.DestroyMenu();

                         delete lpoint;

                  }

                  break;

          case WM_LBUTTONDBLCLK:                                 // 双击左键的处理

                  {

                        this->ShowWindow(SW_SHOWNORMAL);         // 显示主窗口

                                                         this->SetForegroundWindow();

                  }

                  break;

           }

          return 0;

    }

void CDeleteDlg::OnDestroy()

{

   Shell_NotifyIcon(NIM_DELETE, &m_nid);                // 在托盘区删除图标

   CDialog::OnDestroy();

        

         //TODO: Add your message handler code here

        

}

void CDeleteDlg::OnRadio3()

{

         //TODO: Add your control notification handler code here

         m_radio3= 0;

}

void CDeleteDlg::OnRadio4()

{

         //TODO: Add your control notification handler code here

         m_radio3= 1;

}

       


猜你喜欢

转载自blog.csdn.net/dick2737/article/details/38948311
今日推荐