Unity 遍历目录内所有文件并拷贝到指定目录

11 篇文章 0 订阅

遍历目录内所有文件并拷贝到指定目录

🍨程序

🥗打包程序

🍭不重名版本

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class CopyFiles2OtherDir : MonoBehaviour
{
    public string InputDir { get; private set; }
    public string OutputDir { get; private set; }
    public string Extension { get; private set; }

    public UnityEngine.Events.UnityEvent<string> LogEvent;
    StringBuilder sb=new StringBuilder();
    public void Execute()
    {
        if (Directory.Exists(InputDir) == false)
        {
            Log(msg:$"读取目录:{InputDir} 非法");
            return;
        }
        if (Directory.Exists(OutputDir) == false)
        {
            Log($"输出目录:{OutputDir} 非法");
            return;
        }
        List<FileInfo> fileList = new List<FileInfo>();
        DirectoryInfo dInfo = new DirectoryInfo(InputDir);
        GetAllFiles(dir: dInfo, fileList: fileList);
        Log($"一共有:{fileList.Count}{Extension}类型的文件");
        Copy2TargetDir(fileList,OutputDir);
    }

    private void Log(string msg)
    {
        sb.Append(msg+"\n");
        LogEvent?.Invoke(sb.ToString());
        Debug.Log(msg);
    }

    private void Copy2TargetDir(List<FileInfo> fileList, string outputDir)
    {
        string first = "";
        fileList.ForEach(fInfo => {
            string fname = fInfo.Name;
            fname = $"{outputDir}\\{fname}";
            if (File.Exists(fname))
            {
                File.Delete(fname);
            }
            fInfo.CopyTo(fname);
            if (string.IsNullOrEmpty(first))
            {
                first = fname;
            }
            Log($"{fInfo.FullName} 已拷贝到:{fname}");
        });
        System.Diagnostics.Process.Start(Path.GetDirectoryName(first));
    }

    private void GetAllFiles(DirectoryInfo dir, List<FileInfo> fileList)
    {
        foreach (var item in dir.GetFiles())
        {
            if (string.IsNullOrEmpty(Extension) || item.Extension.Equals(Extension))
            {
                fileList.Add(item);
            }
            else
            {
                Log($"{item.FullName} 不是目标文件类型");
            }
        }
        foreach (var item in dir.GetDirectories())
        {
            GetAllFiles(item, fileList);
        }
    }

    public void UpdateInputDir(string inputDir)
    {
        this.InputDir = inputDir;
    }

    public void UpdateOutputDir(string outputDir)
    {
        this.OutputDir = outputDir;
    }

    public void UpdateTargetExtension(string extension)
    {
        this.Extension = extension;
    }

}

🍔重命名为文件夹名称版本

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class CopyFiles2OtherDir : MonoBehaviour
{
    public string InputDir { get; private set; }
    public string OutputDir { get; private set; }
    public string Extension { get; private set; }

    public UnityEngine.Events.UnityEvent<string> LogEvent;
    StringBuilder sb=new StringBuilder();
    public void Execute()
    {
        if (Directory.Exists(InputDir) == false)
        {
            Log(msg:$"读取目录:{InputDir} 非法");
            return;
        }
        if (Directory.Exists(OutputDir) == false)
        {
            Log($"输出目录:{OutputDir} 非法");
            return;
        }
        List<FileInfo> fileList = new List<FileInfo>();
        DirectoryInfo dInfo = new DirectoryInfo(InputDir);
        GetAllFiles(dir: dInfo, fileList: fileList);
        Log($"一共有:{fileList.Count}{Extension}类型的文件");
        Copy2TargetDir(fileList,OutputDir);
    }

    private void Log(string msg)
    {
        sb.Append(msg+"\n");
        LogEvent?.Invoke(sb.ToString());
        Debug.Log(msg);
    }

    private void Copy2TargetDir(List<FileInfo> fileList, string outputDir)
    {
        string first = "";
        fileList.ForEach(fInfo => {
            string fname = fInfo.Name;
            fname = $"{outputDir}\\{fInfo.Directory.Name}{fInfo.Extension}";
            if (File.Exists(fname))
            {
                File.Delete(fname);
            }
            fInfo.CopyTo(fname);
            if (string.IsNullOrEmpty(first))
            {
                first = fname;
            }
            Log($"{fInfo.FullName} 已拷贝到:{fname}");
        });
        System.Diagnostics.Process.Start(Path.GetDirectoryName(first));
    }

    private void GetAllFiles(DirectoryInfo dir, List<FileInfo> fileList)
    {
        foreach (var item in dir.GetFiles())
        {
            if (string.IsNullOrEmpty(Extension) || item.Extension.Equals(Extension))
            {
                fileList.Add(item);
            }
            else
            {
                Log($"{item.FullName} 不是目标文件类型");
            }
        }
        foreach (var item in dir.GetDirectories())
        {
            GetAllFiles(item, fileList);
        }
    }

    public void UpdateInputDir(string inputDir)
    {
        this.InputDir = inputDir;
    }

    public void UpdateOutputDir(string outputDir)
    {
        this.OutputDir = outputDir;
    }

    public void UpdateTargetExtension(string extension)
    {
        this.Extension = extension;
    }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牙膏上的小苏打2333

哟,哟,切克闹,煎饼果子来一套

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值