VirtualFramework/Assets/Scripts/Editor/LowercaseFileRenamer.cs

145 lines
4.8 KiB
C#
Raw Normal View History

#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using System.IO;
using System;
public class WindowsLowercaseRenamer : EditorWindow
{
[MenuItem("Assets/Windows<77><73>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")]
private static void SafeRenameAll()
{
string dataPath = Global.dataPath;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD>
ProcessRootDirectory(dataPath);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC><EFBFBD>ļ<EFBFBD>
ProcessDirectoriesRecursive(dataPath);
ProcessFilesRecursive(dataPath);
AssetDatabase.Refresh();
Debug.Log("<22><>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD>");
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼
private static void ProcessRootDirectory(string originalPath)
{
try
{
DirectoryInfo rootInfo = new DirectoryInfo(originalPath);
string targetRootName = rootInfo.Name.ToLowerInvariant();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC><EFBFBD>Ʋ<EFBFBD><C6B2><EFBFBD><EFBFBD>Ϲ
if (rootInfo.Name != targetRootName)
{
string parentPath = rootInfo.Parent.FullName;
string newRootPath = Path.Combine(parentPath, targetRootName);
// <20><><EFBFBD><EFBFBD><E2B4A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>Ȩ<EFBFBD><C8A8>
if (Directory.Exists(newRootPath))
{
string tempPath = Path.Combine(parentPath, Guid.NewGuid().ToString());
Directory.Move(originalPath, tempPath);
Directory.Move(tempPath, newRootPath);
}
else
{
Directory.Move(originalPath, newRootPath);
}
Debug.Log($"<22><>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>: {originalPath} <20><> {newRootPath}");
// <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>
Global.dataPath = newRootPath; // <20><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>Global<61><6C><EFBFBD><EFBFBD>ʵ<EFBFBD>ֿ<EFBFBD><D6BF><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
}
}
catch (Exception ex)
{
Debug.LogError($"<22><>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>: {ex.Message}");
}
}
private static void ProcessDirectoriesRecursive(string path)
{
foreach (var dirPath in Directory.GetDirectories(path))
{
// <20>ݹ鴦<DDB9><E9B4A6><EFBFBD><EFBFBD>Ŀ¼
ProcessDirectoriesRecursive(dirPath);
DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
string targetName = dirInfo.Name.ToLowerInvariant();
// <20><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϲ淶ʱ<E6B7B6><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (dirInfo.Name != targetName)
{
string parentPath = dirInfo.Parent.FullName;
string newPath = Path.Combine(parentPath, targetName);
try
{
// <20><><EFBFBD><EFBFBD><E2B4A6><EFBFBD><EFBFBD>Windows<77><73>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
//if (Directory.Exists(newPath))
//{
// string tempPath = Path.Combine(parentPath, Guid.NewGuid().ToString());
// Directory.Move(dirPath, tempPath);
// Directory.Move(tempPath, newPath);
//}
//else
//{
Directory.Move(dirPath, newPath);
//}
Debug.Log($"Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>: {dirPath} <20><> {newPath}");
}
catch (Exception ex)
{
Debug.LogError($"Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>: {dirPath}\n{ex.Message}");
}
}
}
}
private static void ProcessFilesRecursive(string path)
{
foreach (var filePath in Directory.GetFiles(path))
{
FileInfo fileInfo = new FileInfo(filePath);
string targetName = Path.GetFileNameWithoutExtension(fileInfo.Name).ToLowerInvariant();
string targetExt = fileInfo.Extension.ToLowerInvariant();
string newFileName = $"{targetName}{targetExt}";
// <20>Ƚ<EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
if (fileInfo.Name != newFileName)
{
string newPath = Path.Combine(fileInfo.DirectoryName, newFileName);
try
{
// Windows<77><73><EFBFBD><EFBFBD><E2B4A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>Сд<D0A1><D0B4>ͬ
if (File.Exists(newPath))
{
string tempPath = Path.Combine(fileInfo.DirectoryName, Guid.NewGuid().ToString() + fileInfo.Extension);
File.Move(filePath, tempPath);
File.Move(tempPath, newPath);
}
else
{
File.Move(filePath, newPath);
}
Debug.Log($"<22>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>: {filePath} <20><> {newPath}");
}
catch (Exception ex)
{
Debug.LogError($"<22>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>: {filePath}\n{ex.Message}");
}
}
}
// <20>ݹ<EFBFBD><DDB9><EFBFBD>Ŀ¼
foreach (var dirPath in Directory.GetDirectories(path))
{
ProcessFilesRecursive(dirPath);
}
}
}
#endif