zoukankan      html  css  js  c++  java
  • 检查DIsk中的空文件夹

    公司里边有一条test case是检查提供给客户的文件里边没有空的文件夹, 我自己就做了个小工具来跑这条 case. 基础就是利用.NET 3.5中的SystemFileInfo类来调区disk上的信息:

    界面如下:

    代码如下:

    代码
    namespace CheckEmpty
    {
        
    public partial class CheckEmpty : Form
        {
            
    public CheckEmpty()
            {
                InitializeComponent();
            }

            
    private void Open_Click(object sender, EventArgs e)
            {

                oFDialog.ShowDialog();
                
    if (DialogResult.Cancel != oFDialog.ShowDialog())
                {
                    CheckPath.Text 
    = oFDialog.FileName;
                }
            }

            
    private void Save_Click(object sender, EventArgs e)
            {
                
    if (DialogResult.Cancel != sFDialog.ShowDialog())
                {
                    outPutPath.Text 
    = sFDialog.FileName;
                }
            }

            
    private void Check_Click(object sender, EventArgs e)
            {
                
    if (CheckPath.Text.Trim() != "" && outPutPath.Text.Trim() != "")
                {
                    
    try
                    {
                        Errlbl.Text 
    = "";
                        toolStripProgressBar1.Value 
    = 0;
                        
    string orfile = CheckPath.Text.Trim();
                        
    string reportPath = outPutPath.Text.Trim();
                        
    if (!File.Exists(reportPath))
                        {
                            
    // Errlbl.Text = "Output File path not exist";
                            FileStream fs = File.Create(reportPath);
                            fs.Close();
                        }
                        
    int MaxCount = 0;
                        
    int EmptyCount = 0;
                        
    //Count total folders
                        CountfiletoScan(orfile, ref MaxCount);
                        
    if (MaxCount != 0)
                        {
                            toolStripProgressBar1.Maximum 
    = MaxCount;
                        }
                        StreamWriter sw 
    = File.CreateText(reportPath);
                        sw.WriteLine(
    string.Format("Following folder are empty: (Total folders: {0})", MaxCount));
                        
    // Scan empty folders
                        RecursiveScan(orfile, sw, ref EmptyCount);
                        sw.Close();
                        sw.Dispose();
                        Errlbl.ForeColor 
    = Color.Red;
                        
    if (MaxCount != 0)
                        {
                            Errlbl.Text 
    = string.Format(@"Completed! emptys({0})", EmptyCount);
                        }
                        
    else
                        {
                            Errlbl.Text 
    = "Completed! Err";
                        }
                        Clipboard.SetDataObject(reportPath); 
    //Copy output report to clipboard
                    }
                    
    catch { }
                }
                
    else
                {
                    Errlbl.Text 
    = "Both Folder and Report can not be empty";
                }
            }

            
    private void RecursiveScan(string dir, StreamWriter sw, ref int emptycount)
            {
                DirectoryInfo di 
    = new DirectoryInfo(dir);
                
    if (!di.Exists)
                {
                    
    //Original directory not exist
                    sw.WriteLine(string.Format (@"dirctory '{0}' not exist",di.FullName));
                    sw.Flush();
                    toolStripProgressBar1.Value 
    = 100;
                }
                
    else
                {
                    
    try
                    {
                        FileSystemInfo[] fsi 
    = di.GetFileSystemInfos();
                        toolStripProgressBar1.Value 
    += di.GetDirectories().Length;
                        
    if (fsi.Length == 0)
                        {
                            
    //Output empty direcotry
                            sw.WriteLine(di.FullName);
                            sw.Flush();
                            emptycount
    ++;
                        }
                        
    else
                        {
                            
    foreach (DirectoryInfo item in di.GetDirectories())
                            {
                                RecursiveScan(item.FullName, sw, 
    ref emptycount);
                            }
                        }
                    }
                    
    catch (FieldAccessException fae)
                    {
                        
    //
                    }
                    
    catch (Exception ex)
                    {
                        
    //
                    }
                }
            }

            
    private void CountfiletoScan(string dir, ref int count)
            {
                DirectoryInfo di 
    = new DirectoryInfo(dir);
                
    if (!di.Exists)
                {                
                }
                
    else
                {
                    
    try
                    {
                        DirectoryInfo[] dis 
    = di.GetDirectories();
                        count 
    += dis.Length;
                        
    foreach (DirectoryInfo item in dis)
                        {
                            CountfiletoScan(item.FullName, 
    ref count);
                        }
                    }
                    
    catch
                    { }
                }
            }
        }
    }
  • 相关阅读:
    Oracle数据库——半期测验
    Oracle数据库——SQL高级查询
    mysql中整数类型后面的数字,是不是指定这个字段的长度?比如int(11),11代表11个字节吗?
    ehcache memcache redis 三大缓存男高音
    Java Redis Pipeline 使用示例
    游族网络:我们是怎么玩转千台以上游戏云服务器的
    java 在Excel中插入图片 POI实现
    解放运维的双手,谈自动化运维管理平台设计
    运维堡垒机
    查询相应的key
  • 原文地址:https://www.cnblogs.com/qixue/p/1788894.html
Copyright © 2011-2022 走看看