🍨程序
🥗打包程序
🍭不重名版本
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;
}
}