比如有很多文件,想删除一些隔几周未用的文件。

解决方案 »

  1.   

    遍历你需要查找的位置的文件,并用GetFileTime获取文件最后访问时间,计算与当前时间间隔,删除。
      

  2.   

    可以使用 CFileFind 类查找指定目录下的文件,然后使用 CFileFind::GetCreationTime(创建时间)、CFileFind::GetLastAccessTime(最后访问时间)、CFileFind::GetLastWriteTime(最后修改时间),获取文件的时间,最后使用 DeleteFile 删除文件。
      

  3.   

    这是删除C:\1\下面创建时间小于2009-2-23 18:50 1秒的CFileFind  ff;                       
    CString  szDir="c:\\1\\*.*";         
    BOOL res=ff.FindFile(szDir);         
    while(res)         
    {   
    res=ff.FindNextFile();         
        if(!ff.IsDirectory()&&!ff.IsDots())         
    {
    CTime fTime; CTime time(2009,2,23,18,50,1);
    ff.GetCreationTime(fTime);
    if(fTime<time)
    {
    CString path="c:\\1\\";
    path+=ff.GetFileName();
    DeleteFile(path);
    }
    }         
    }  
    ff.Close();
    }
      

  4.   

    我也在做这样的程序呢。我写了一部分
    #include "stdafx.h"
    #include "delfilesonTime.h"
    #include "delfilesonTimeDlg.h"
    #include "BrowseDirDialog.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
    {
    public:
    CAboutDlg();
    // LPITEMIDLIST SHBrowseForFolder(
    // LPBROWSEINFO lpbi; 
    // 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()/////////////////////////////////////////////////////////////////////////////
    // CDelfilesonTimeDlg dialogCDelfilesonTimeDlg::CDelfilesonTimeDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CDelfilesonTimeDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CDelfilesonTimeDlg)
    m_ShPath = _T("");
    m_Seltime = 0;
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }void CDelfilesonTimeDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CDelfilesonTimeDlg)
    DDX_Text(pDX, IDC_STATICPATH, m_ShPath);
    DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER1, m_Seltime);
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CDelfilesonTimeDlg, CDialog)
    //{{AFX_MSG_MAP(CDelfilesonTimeDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BTN_SELFOLD, OnBtnSelfold)
    ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CDelfilesonTimeDlg message handlersBOOL CDelfilesonTimeDlg::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 CDelfilesonTimeDlg::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 CDelfilesonTimeDlg::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 CDelfilesonTimeDlg::OnQueryDragIcon()
    {
    return (HCURSOR) m_hIcon;
    }void CDelfilesonTimeDlg::OnBtnSelfold() 
    {
    // TODO: Add your control notification handler code here
        ::CBrowseDirDialog brdir;

    brdir.DoBrowse();
    m_SelFolder=brdir.m_Path;
        m_ShPath=m_SelFolder;
    UpdateData(FALSE);

        }void CDelfilesonTimeDlg::OnButton2() 
    {
    // TODO: Add your control notification handler code here
    //查找目录下指定时间以前的文件
        CString strDir=m_SelFolder; int nNum=SearchFile(m_SelFolder);
    CString str="";
    str.Format("该文件夹下文件数为%d!",nNum);
    MessageBox(str);
     }
    int CDelfilesonTimeDlg::SearchFile(CString strDir)
    {
        CFileFind filefind;
    int m_nNum=0;
    CString szDir=strDir; //文件目录
        if (szDir.Right(1)!="\\")
    szDir+="\\";
    szDir+="*.*";
    BOOL bRes=filefind.FindFile(szDir);
    while(bRes)
    {
    bRes=filefind.FindNextFile();
    // if(filefind.GetCreationTime<m_Seltime)
    if(!filefind.IsDirectory()&&!filefind.IsDots())
    {
       if(filefind.GetCreationTime<m_Seltime)
               m_nNum++;
            }
    else if (filefind.IsDirectory()&&!filefind.IsDots())
    SearchFile(filefind.GetFilePath());
    }
    filefind.Close();
    return m_nNum;
    }
    但现在问题是比对时间报错。用的vc中自带的date time picker控件,帮定了一个变量,在和filefind.get creationtime比较时报错,再者,删除功能还没实现。
      

  5.   

    yuzhongzhu帮了大忙了,不过再改进一下变成“删除与当前系统时间相隔特定时间(比如说一星期)的文件”用CTime类中的“-”操作来判断减之后的结果是否大于7天从而来删除文件也应该能够实现吧