指定文件目录下的文件账龄分析及删除

指定文件目录下的文件账龄分析及删除

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace JACKTools
{

    public partial class FileLibAge : Form
    {
        private List<FileAge> lst = new List<FileAge>();
        private const double KB = 1024;
        private const double MB = 1024 *  1024;
        private const double GB = MB * 1024;
        private const double TB = GB * 1024;   

        public FileLibAge()
        {
            InitializeComponent();
        }

        private void btn_tj_Click(object sender, EventArgs e)
        {
            //目录路径
            lst.Clear();
            lsv_show.Items.Clear();
            String path = txt_dir.Text.Trim();
            countfileage(path);
            lst.Sort(new FileAge());
            for (int i = 0; i < lst.Count; i++)
            {
                String size = "";
                if (lst[i].filesize > TB)
                    size = Math.Round(lst[i].filesize / TB ,2)+ "TB";
                else if (lst[i].filesize > GB)
                    size = Math.Round(lst[i].filesize / GB ,2)+ "GB";
                else if (lst[i].filesize > MB)
                    size = Math.Round(lst[i].filesize  / MB ,2)+ "MB";
                else if (lst[i].filesize > KB)
                    size = Math.Round(lst[i].filesize / KB ,2)+ "KB";
                else size = lst[i].filesize + "B";
                lsv_show.Items.Add(new ListViewItem(new String[] { lst[i].year + "", lst[i].filecnt + "", size }));
            }
        }
        private void countfileage(string filedir)
        { 
            DirectoryInfo root = new DirectoryInfo(filedir);
            foreach (FileInfo f in root.GetFiles())
            {
                //
                if (File.Exists(f.FullName))
                {
                    int year = f.LastWriteTime.Year;
                    if(rdn_month.Checked)
                        year = f.LastWriteTime.Year * 100 + f.LastWriteTime.Month;
                    else
                        year = f.LastWriteTime.Year;
                    int iidex = getindexoflsv(year);
                    if (iidex >= 0)
                    {
                        lst[iidex].filecnt++;
                        lst[iidex].filesize += f.Length;
                    }
                    else
                    {
                        FileAge tmp = new FileAge();
                        tmp.year = year;
                        tmp.filecnt = 1;
                        tmp.filesize = f.Length;
                        lst.Add(tmp);
                    }
                }
            }
            foreach (DirectoryInfo d in root.GetDirectories())
                countfileage(d.FullName);
        }

        private int getindexoflsv(int year)
        {
            for (int i = 0; i < lst.Count; i++)
            {
                if (lst[i].year == year)
                    return i;
            }
            return -1;
        }

        private void btnclear_Click(object sender, EventArgs e)
        {
            if (DateTime.Now.Year - dtpend.Value.Year <= 1)
            {
                MessageBox.Show("1年内的数据不删除!");
                return;
            }
            else
            {
                String path = txt_dir.Text.Trim();
                cleardata(path);
                MessageBox.Show("路径" + path + "下历史数据已经清理完毕!");
            }
        }

        /// <summary>
        /// 清理制定日期前的制定目录下的文件
        /// </summary>
        /// <param name="path"></param>
        private  void cleardata(String path)
        {
            DirectoryInfo root = new DirectoryInfo(path);
            foreach (FileInfo f in root.GetFiles())
            {
                try
                {
                    if (f.LastWriteTime < dtpend.Value)
                        f.Delete();
                }
                catch (Exception ee)
                { 
                
                }
            }
            foreach (DirectoryInfo d in root.GetDirectories())
                cleardata(d.FullName);
            //路径下无文件 则直接删除目录
            if (root.GetFiles().Length <= 0 && root.GetDirectories().Length <= 0)
            {
                root.Delete();
            }
        }
    }

    public class FileAge : IComparer<FileAge>
    {
        public int year
        {
            get;
            set;
        }

        public int filecnt
        {
            get;
            set;

        }

        public double filesize
        {
            get;
            set;
        }

        //Compare函数
        public int Compare(FileAge x, FileAge y)
        {
            try
            {
                if (x == null || y == null)
                    return 0;
                return x.year.CompareTo(y.year);//升序
            }
            catch (Exception ee)
            { }
            return 0;

        }

    }
}

 

上一篇:C#字符串常用方法


下一篇:2021-09-15