1. 程式人生 > >定時刪除伺服器檔案--FTP管理員一定用得著的軟體

定時刪除伺服器檔案--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;

}