zoukankan      html  css  js  c++  java
  • 递归遍历文件及子文件夹下的文件过滤无关文件对文件进行复制(该代码是复制过来修改过的,如果有侵作者权的话,请作者联系我,立即删除)

    namespace Demo
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            public string source;
            public string target;
            public int count=1;
            private void btn_StartCopy_Click(object sender, EventArgs e)
            {
    
                if (string.IsNullOrEmpty(txtSource.Text)||string.IsNullOrEmpty(txtTarget.Text))
                {
                    MessageBox.Show("路径不能为空!"); 
                }
                else
                {
                    source = txtSource.Text;//原路径
    
                    target = txtTarget.Text;//目标路径
    
    
                    try
                    {
                        ListFiles(new DirectoryInfo(source));
                            MessageBox.Show("复制完成"+count+"文件"); 
                    }
                    catch (IOException  ex)
                    {
                        
                        Console.WriteLine(ex.Message);
                    }
    
                }
                
    
            }
    
           /// <summary>
            /// //递归遍历所有文件包括子文件夹下的文件 并对word excel pdf文件进行复制到目标路径
           /// </summary>
           /// <param name="info"></param>
            public  void ListFiles(FileSystemInfo info)
            {
                if (!info.Exists) { return; }
    
                DirectoryInfo dir = info as DirectoryInfo;
                //不是目录 
                if (dir == null) { return; }
    
    
                FileSystemInfo[] files = dir.GetFileSystemInfos();
                for (int i = 0; i < files.Length; i++)
                {
                    FileInfo file = files[i] as FileInfo;
                    //是文件 
    
                    if (file != null)
                    {
                        string[] arr = file.Name.Split(new char[] { '.' });
    
                        arr[arr.Length - 1] = arr[arr.Length - 1].ToLower();
    
                        if (arr[arr.Length - 1] == "doc" || arr[arr.Length - 1] == "docx")
                        {
                            
                            //复制文件 (为true是覆盖同名文件)
                            File.Copy(file.DirectoryName + @"\" + file.Name, Path.Combine(target, file.Name), true);
                            count++;
                        }
                        if (arr[arr.Length - 1] == "xls" || arr[arr.Length - 1] == "xlsx")
                        {
                            //复制文件 (为true是覆盖同名文件)
                            File.Copy(file.DirectoryName + @"\" + file.Name, Path.Combine(target, file.Name), true);
                            count++;
                        }
                        if (arr[arr.Length - 1] == "pdf")
                        {
                            //复制文件 (为true是覆盖同名文件)
                            File.Copy(file.DirectoryName + @"\" + file.Name, Path.Combine(target, file.Name), true);
                            count++;
                        }
                    }
                    //对于子目录,进行递归调用 
                    else
                    {
                        ListFiles(files[i]);
                    }
                }
    
            }
    
    
        }
    }
    

      

  • 相关阅读:
    CentOS开发环境LAMP搭建
    Oracle中*.dpm文件导入
    SQL Server查询数据库中所有的表名及行数
    SQL Server数据库同步SQL
    Vim 快捷键整理
    SQL Server解决死锁问题
    python重试装饰器的简单实现
    神奇的描述符(三):覆盖描述符与非覆盖描述符
    神奇的描述符(二):使用描述符实现实例属性的类型检查
    神奇的描述符(一):描述符协议的实现
  • 原文地址:https://www.cnblogs.com/zjw520/p/3014756.html
Copyright © 2011-2022 走看看