405 lines
14 KiB
PowerShell
405 lines
14 KiB
PowerShell
# 中文文件/目录名重命名为英文
|
||
# 功能:
|
||
# 1. 扫描所有中文文件和目录
|
||
# 2. 生成英文重命名映射表
|
||
# 3. 执行重命名
|
||
# 4. 更新所有引用链接
|
||
|
||
param(
|
||
[switch]$DryRun = $false, # 默认预览模式
|
||
[switch]$Execute = $false, # 执行模式
|
||
[int]$MaxLength = 50 # 文件名最大长度
|
||
)
|
||
|
||
$ErrorActionPreference = "Stop"
|
||
$mappingFile = ".\chinese-to-english-mapping.csv"
|
||
|
||
# 中文到英文的翻译字典(常用词汇)
|
||
$translationDict = @{
|
||
# 项目相关
|
||
"广汽" = "GAC"
|
||
"物联网" = "IoT"
|
||
"AIOT" = "AIoT"
|
||
"自动驾驶" = "Autonomous-Driving"
|
||
"从化应急" = "Conghua-Emergency"
|
||
"公文交换" = "Document-Exchange"
|
||
"商务局" = "Commerce-Bureau"
|
||
"市发改委" = "Municipal-Development-Reform"
|
||
"虚拟数据中心" = "Virtual-Data-Center"
|
||
"增城区" = "Zengcheng-District"
|
||
"番禺区" = "Panyu-District"
|
||
"工信" = "Industry-Info"
|
||
"道路秩序" = "Road-Order"
|
||
"南电" = "CSG" # China Southern Grid
|
||
"南方电网" = "China-Southern-Grid"
|
||
|
||
# 文件夹名称
|
||
"城中村" = "Urban-Village"
|
||
"番禺城中村" = "Panyu-Urban-Village"
|
||
"番禺公文" = "Panyu-Document"
|
||
"番禺信用" = "Panyu-Credit"
|
||
"番禺住建" = "Panyu-Housing"
|
||
"公文" = "Document"
|
||
"信用" = "Credit"
|
||
"住建" = "Housing"
|
||
"增城区信息化项目管理系统" = "Zengcheng-IT-Project-Mgmt"
|
||
|
||
# 文件名关键词
|
||
"培训记录整理" = "Training-Records"
|
||
"设备添加" = "Device-Addition"
|
||
"测试环境" = "Test-Environment"
|
||
"维护" = "Maintenance"
|
||
"数据流向" = "Data-Flow"
|
||
"系统架构" = "System-Architecture"
|
||
"登陆" = "Login"
|
||
"配置" = "Configuration"
|
||
"问题处理" = "Issue-Handling"
|
||
"处理" = "Handling"
|
||
"升级" = "Upgrade"
|
||
"漏洞处理" = "Vulnerability-Fix"
|
||
"达梦" = "DM-Database"
|
||
"等保问题描述" = "Security-Issues"
|
||
"等保修复" = "Security-Fixes"
|
||
"基线" = "Baseline"
|
||
"重建主节点" = "Rebuild-Master-Node"
|
||
"设备电源" = "Device-Power"
|
||
"智谱清言" = "ChatGLM"
|
||
"热点升级" = "Hotspot-Upgrade"
|
||
"自行车" = "Bicycle"
|
||
"酸黄瓜制作" = "Pickled-Cucumber-Recipe"
|
||
"酸黄瓜" = "Pickled-Cucumber"
|
||
"文明" = "Civilization"
|
||
"摩托罗拉" = "Motorola"
|
||
"产出" = "Deliverables"
|
||
"架构目标" = "Architecture-Goals"
|
||
"决策方法" = "Decision-Methods"
|
||
"系统架构分析员知识体系" = "SA-Knowledge-System"
|
||
"芹菜炒牛肉" = "Celery-Beef-Stir-Fry"
|
||
"图片生成提示词模板" = "Image-Prompt-Template"
|
||
|
||
# 通用词汇
|
||
"机场推荐" = "VPN-Airport-Recommendations"
|
||
"软件供应商手册" = "Software-Vendor-Handbook"
|
||
"生成和提供" = "Generation-and-Provision"
|
||
"解读" = "Interpretation"
|
||
"关于" = "About"
|
||
"软件物料清单" = "Software-Bill-of-Materials"
|
||
"你所需要了解的一切" = "Everything-You-Need-to-Know"
|
||
"如何使用" = "How-to-Use"
|
||
"微软的开源工具生成" = "Microsoft-OpenSource-Tools"
|
||
"如何通过" = "How-to-Achieve"
|
||
"实现安全治理" = "Security-Governance"
|
||
"联想移动互联及数字家庭产品服务支持" = "Lenovo-Mobile-Digital-Home-Support"
|
||
"需要考虑的" = "Top"
|
||
"种顶级" = ""
|
||
"工具" = "Tools"
|
||
"中文网" = "CN"
|
||
"如何购买" = "How-to-Purchase"
|
||
"分钟搞定" = "Quick-Guide"
|
||
"功能强大" = "Powerful"
|
||
"开源项目" = "Open-Source-Project"
|
||
"创造令人愉悦的身份体验" = "Delightful-Identity-Experience"
|
||
"深度学习雾计算平台" = "Deep-Learning-Fog-Platform"
|
||
"开户教程" = "Account-Opening-Guide"
|
||
"一张数据量很大的表" = "Large-Data-Table"
|
||
"分页查询特别耗时" = "Slow-Pagination-Query"
|
||
"你有什么优化方案" = "Optimization-Solutions"
|
||
"实现" = "Implementation"
|
||
"拨号与" = "Dialing-and"
|
||
"机场评测" = "VPN-Reviews"
|
||
"订阅" = "Subscription"
|
||
"报文简介" = "Message-Intro"
|
||
"博客" = "Blog"
|
||
"国内镜像源列表" = "China-Mirror-List"
|
||
"年" = ""
|
||
"月" = ""
|
||
"日" = ""
|
||
"亲测可用" = "Tested-Working"
|
||
"安装" = "Installation"
|
||
"技术文档" = "Technical-Docs"
|
||
"数据库" = "Database"
|
||
"博客园" = "cnblogs"
|
||
"安装前准备" = "Pre-Installation"
|
||
"配置实例" = "Configure-Instance"
|
||
"离线安装" = "Offline-Installation"
|
||
"数据库安装" = "DB-Installation"
|
||
"技术我的一些" = "My"
|
||
"学习经验" = "Learning-Experience"
|
||
"安装和打包" = "Installation-and-Packaging"
|
||
"权限校验" = "Permission-Validation"
|
||
"官网" = "Official-Site"
|
||
"学习笔记" = "Learning-Notes"
|
||
"应用" = "Application"
|
||
"部署" = "Deployment"
|
||
"单实例数据库" = "Single-Instance-DB"
|
||
"一个" = "A"
|
||
"的工作学习笔记" = "Work-Study-Notes"
|
||
"集成" = "Integration"
|
||
"节点访问局域网服务器的配置方法" = "LAN-Server-Access-Config"
|
||
"公开镜像仓库未授权访问" = "Public-Registry-Unauthorized-Access"
|
||
"数据库从" = "DB-Migration-from"
|
||
"迁移到" = "to"
|
||
"综合讨论区" = "Discussion"
|
||
"智能家居技术论坛" = "Smart-Home-Forum"
|
||
"树莓派" = "Raspberry-Pi"
|
||
"边界路由器配置" = "Border-Router-Config"
|
||
"配置" = "Config"
|
||
"雾凇拼音" = "Rime-Ice"
|
||
"长期维护的简体词库" = "Simplified-Chinese-Dict"
|
||
"中国申请" = "China-Application"
|
||
"中国区免费" = "China-Free"
|
||
"使用攻略" = "Usage-Guide"
|
||
"云" = "Cloud"
|
||
"详细" = "Detailed"
|
||
"注册申请图文教程" = "Registration-Tutorial"
|
||
"整合" = "Integration"
|
||
"软件开发实施" = "Software-Implementation"
|
||
"服装生鲜家具外贸供应链开源" = "Apparel-Food-Furniture-Supply-Chain"
|
||
"专业实施" = "Pro-Implementation"
|
||
"什麼是" = "What-is"
|
||
"軟體物料清單" = "SBOM"
|
||
"網路安全解決方案" = "Network-Security-Solutions"
|
||
"作为旁路网关" = "as-Bypass-Gateway"
|
||
"不是旁路由" = "not-Bypass-Router"
|
||
"单臂路由" = "One-Armed-Router"
|
||
"的终极设置方法" = "Ultimate-Setup"
|
||
"破解迷思" = "Debunking-Myths"
|
||
"少数派" = "sspai"
|
||
"知乎" = "Zhihu"
|
||
"掘金" = "Juejin"
|
||
}
|
||
|
||
# 拼音转换函数(简化版,使用首字母)
|
||
function Get-Pinyin {
|
||
param([string]$Text)
|
||
|
||
# 先尝试从字典查找
|
||
if ($translationDict.ContainsKey($Text)) {
|
||
return $translationDict[$Text]
|
||
}
|
||
|
||
# 检查是否有部分匹配
|
||
foreach ($key in $translationDict.Keys) {
|
||
if ($Text.Contains($key)) {
|
||
$Text = $Text.Replace($key, $translationDict[$key])
|
||
}
|
||
}
|
||
|
||
# 如果还有中文,返回原文(后续手动处理)
|
||
if ($Text -match '[\u4e00-\u9fa5]') {
|
||
# 简单清理:移除特殊字符
|
||
$Text = $Text -replace '[:。,、;!?""''()【】《》\s]+', '-'
|
||
$Text = $Text -replace '^-+|-+$', ''
|
||
}
|
||
|
||
return $Text
|
||
}
|
||
|
||
# 规范化文件名(移除非法字符,控制长度)
|
||
function Normalize-FileName {
|
||
param([string]$Name, [string]$Extension = "")
|
||
|
||
# 移除或替换非法字符
|
||
$Name = $Name -replace '[<>:"/\\|?*]', '-'
|
||
$Name = $Name -replace '\s+', '-'
|
||
$Name = $Name -replace '-+', '-'
|
||
$Name = $Name -replace '^-+|-+$', ''
|
||
|
||
# 控制长度
|
||
if ($Name.Length -gt $MaxLength) {
|
||
$Name = $Name.Substring(0, $MaxLength)
|
||
$Name = $Name -replace '-+$', ''
|
||
}
|
||
|
||
if ($Extension) {
|
||
return "$Name$Extension"
|
||
}
|
||
return $Name
|
||
}
|
||
|
||
Write-Host "=== 中文文件名重命名工具 ===" -ForegroundColor Cyan
|
||
Write-Host ""
|
||
|
||
if ($Execute) {
|
||
Write-Host "⚠️ 执行模式:将真实重命名文件并更新链接" -ForegroundColor Yellow
|
||
} else {
|
||
Write-Host "👁️ 预览模式:仅生成映射表,不执行重命名" -ForegroundColor Green
|
||
Write-Host " 使用 -Execute 参数执行真实重命名" -ForegroundColor Gray
|
||
}
|
||
Write-Host ""
|
||
|
||
# 1. 扫描所有中文文件和目录
|
||
Write-Host "📁 扫描中文文件和目录..." -ForegroundColor Cyan
|
||
|
||
$chineseFiles = Get-ChildItem -Recurse -File | Where-Object { $_.Name -match '[\u4e00-\u9fa5]' }
|
||
$chineseDirs = Get-ChildItem -Recurse -Directory | Where-Object { $_.Name -match '[\u4e00-\u9fa5]' } | Sort-Object FullName -Descending
|
||
|
||
Write-Host " 找到 $($chineseFiles.Count) 个中文文件" -ForegroundColor White
|
||
Write-Host " 找到 $($chineseDirs.Count) 个中文目录" -ForegroundColor White
|
||
Write-Host ""
|
||
|
||
# 2. 生成重命名映射
|
||
Write-Host "📝 生成重命名映射..." -ForegroundColor Cyan
|
||
|
||
$mappings = @()
|
||
|
||
# 处理目录(从深层到浅层,避免父目录先改名导致路径失效)
|
||
foreach ($dir in $chineseDirs) {
|
||
$oldName = $dir.Name
|
||
$newName = Get-Pinyin -Text $oldName
|
||
$newName = Normalize-FileName -Name $newName
|
||
|
||
$relPath = $dir.FullName.Replace((Get-Location).Path, ".").Replace("\", "/")
|
||
$parentPath = Split-Path $relPath -Parent
|
||
$newPath = Join-Path $parentPath $newName
|
||
|
||
$mappings += [PSCustomObject]@{
|
||
Type = "Directory"
|
||
OldPath = $relPath
|
||
NewPath = $newPath
|
||
OldName = $oldName
|
||
NewName = $newName
|
||
}
|
||
}
|
||
|
||
# 处理文件
|
||
foreach ($file in $chineseFiles) {
|
||
$oldName = $file.BaseName
|
||
$extension = $file.Extension
|
||
$newName = Get-Pinyin -Text $oldName
|
||
$newName = Normalize-FileName -Name $newName -Extension $extension
|
||
|
||
$relPath = $file.FullName.Replace((Get-Location).Path, ".").Replace("\", "/")
|
||
$parentPath = Split-Path $relPath -Parent
|
||
$newPath = Join-Path $parentPath $newName
|
||
|
||
$mappings += [PSCustomObject]@{
|
||
Type = "File"
|
||
OldPath = $relPath
|
||
NewPath = $newPath
|
||
OldName = $file.Name
|
||
NewName = $newName
|
||
}
|
||
}
|
||
|
||
# 保存映射表
|
||
$mappings | Export-Csv -Path $mappingFile -Encoding UTF8 -NoTypeInformation
|
||
Write-Host " ✅ 映射表已保存到: $mappingFile" -ForegroundColor Green
|
||
Write-Host " 📊 总计 $($mappings.Count) 个重命名任务" -ForegroundColor White
|
||
Write-Host ""
|
||
|
||
# 3. 显示预览
|
||
Write-Host "📋 重命名预览 (前20项):" -ForegroundColor Cyan
|
||
$mappings | Select-Object -First 20 Type, OldName, NewName | Format-Table -AutoSize
|
||
|
||
if ($mappings.Count -gt 20) {
|
||
Write-Host " ... 还有 $($mappings.Count - 20) 项(查看完整列表: $mappingFile)" -ForegroundColor Gray
|
||
}
|
||
Write-Host ""
|
||
|
||
# 4. 执行重命名(仅在 -Execute 模式)
|
||
if ($Execute) {
|
||
Write-Host "🔄 开始执行重命名..." -ForegroundColor Yellow
|
||
|
||
$renamed = 0
|
||
$failed = 0
|
||
|
||
# 先重命名目录
|
||
foreach ($mapping in ($mappings | Where-Object { $_.Type -eq "Directory" })) {
|
||
try {
|
||
$oldPath = $mapping.OldPath -replace '^\./', ''
|
||
$newPath = $mapping.NewPath -replace '^\./', ''
|
||
|
||
if (Test-Path $oldPath) {
|
||
Rename-Item -Path $oldPath -NewName (Split-Path $newPath -Leaf) -ErrorAction Stop
|
||
Write-Host " ✅ 目录: $($mapping.OldName) → $($mapping.NewName)" -ForegroundColor Green
|
||
$renamed++
|
||
}
|
||
} catch {
|
||
Write-Host " ❌ 失败: $($mapping.OldPath) - $_" -ForegroundColor Red
|
||
$failed++
|
||
}
|
||
}
|
||
|
||
# 再重命名文件
|
||
foreach ($mapping in ($mappings | Where-Object { $_.Type -eq "File" })) {
|
||
try {
|
||
# 更新路径(因为目录可能已改名)
|
||
$oldPath = $mapping.OldPath -replace '^\./', ''
|
||
|
||
# 检查文件是否存在(路径可能因目录改名而变化)
|
||
if (-not (Test-Path $oldPath)) {
|
||
# 尝试查找文件
|
||
$fileName = Split-Path $oldPath -Leaf
|
||
$foundFiles = Get-ChildItem -Recurse -File -Filter $fileName
|
||
if ($foundFiles.Count -eq 1) {
|
||
$oldPath = $foundFiles[0].FullName.Replace((Get-Location).Path + "\", "")
|
||
} else {
|
||
throw "文件未找到或有重复"
|
||
}
|
||
}
|
||
|
||
Rename-Item -Path $oldPath -NewName $mapping.NewName -ErrorAction Stop
|
||
Write-Host " ✅ 文件: $($mapping.OldName) → $($mapping.NewName)" -ForegroundColor Green
|
||
$renamed++
|
||
} catch {
|
||
Write-Host " ❌ 失败: $($mapping.OldPath) - $_" -ForegroundColor Red
|
||
$failed++
|
||
}
|
||
}
|
||
|
||
Write-Host ""
|
||
Write-Host "📊 重命名完成!" -ForegroundColor Cyan
|
||
Write-Host " 成功: $renamed" -ForegroundColor Green
|
||
Write-Host " 失败: $failed" -ForegroundColor Red
|
||
Write-Host ""
|
||
|
||
# 5. 更新链接
|
||
Write-Host "🔗 更新 Markdown 文件中的链接..." -ForegroundColor Cyan
|
||
|
||
$mdFiles = Get-ChildItem -Recurse -Filter "*.md" -File
|
||
$updatedFiles = 0
|
||
|
||
foreach ($mdFile in $mdFiles) {
|
||
$content = Get-Content $mdFile.FullName -Raw -Encoding UTF8
|
||
$originalContent = $content
|
||
$changed = $false
|
||
|
||
# 更新 Wiki 链接 [[中文名]]
|
||
foreach ($mapping in $mappings) {
|
||
$oldBaseName = [System.IO.Path]::GetFileNameWithoutExtension($mapping.OldName)
|
||
$newBaseName = [System.IO.Path]::GetFileNameWithoutExtension($mapping.NewName)
|
||
|
||
if ($content -match "\[\[$oldBaseName\]\]") {
|
||
$content = $content -replace "\[\[$oldBaseName\]\]", "[[$newBaseName]]"
|
||
$changed = $true
|
||
}
|
||
|
||
# 更新路径引用
|
||
$oldPathPattern = [regex]::Escape($mapping.OldPath -replace '^\./', '')
|
||
if ($content -match $oldPathPattern) {
|
||
$newPathEscaped = $mapping.NewPath -replace '^\./', ''
|
||
$content = $content -replace $oldPathPattern, $newPathEscaped
|
||
$changed = $true
|
||
}
|
||
}
|
||
|
||
if ($changed) {
|
||
Set-Content -Path $mdFile.FullName -Value $content -Encoding UTF8 -NoNewline
|
||
$updatedFiles++
|
||
Write-Host " ✅ 更新: $($mdFile.FullName.Replace((Get-Location).Path, '.'))" -ForegroundColor Green
|
||
}
|
||
}
|
||
|
||
Write-Host ""
|
||
Write-Host "📊 链接更新完成!更新了 $updatedFiles 个文件" -ForegroundColor Green
|
||
|
||
} else {
|
||
Write-Host "💡 下一步:" -ForegroundColor Yellow
|
||
Write-Host " 1. 查看映射表: $mappingFile" -ForegroundColor White
|
||
Write-Host " 2. 确认无误后,运行: .\.scripts\rename-chinese-to-english.ps1 -Execute" -ForegroundColor White
|
||
}
|
||
|
||
Write-Host ""
|
||
Write-Host "✨ 完成!" -ForegroundColor Cyan
|