附件大写转小写脚本

来源:本站原创 点击数: 发布时间:2025年10月09日

在网站服务从 Windows 平台迁移或升级到 Linux 平台的过程中,经常会出现文件或路径名称大小写敏感导致的访问异常问题。这是由于 Windows 文件系统默认对文件名不区分大小写,而 Linux 文件系统区分大小写所致。

处理方法,将下方完整代码另存为rename-lowercase.ps1文件,然后在文件目录的powershell执行:

.\Rename-ToLower.ps1 -FolderPath "D:\TestFolder"
param(
    [Parameter(Mandatory=$true)]
    [string]$FolderPath
)
# 检查文件夹是否存在
if (-not (Test-Path -Path $FolderPath -PathType Container)) {
    Write-Error "错误: 指定的文件夹路径不存在: $FolderPath"
    exit 1
}
Write-Host "开始处理文件夹: $FolderPath" -ForegroundColor Green
# ==========================
# 1. 处理文件
# ==========================
$files = Get-ChildItem -Path $FolderPath -File -Recurse
$totalFiles = $files.Count
$processedFiles = 0
$renamedFiles = 0
Write-Host "找到 $totalFiles 个文件..." -ForegroundColor Yellow
foreach ($file in $files) {
    $processedFiles++
    $originalName = $file.Name
    $lowerCaseName = $originalName.ToLower()
    if ($originalName -cne $lowerCaseName) {
        $dirPath = $file.Directory.FullName
        $tempName = ([guid]::NewGuid().ToString() + ".tmp")
        $tempPath = Join-Path -Path $dirPath -ChildPath $tempName
        $targetPath = Join-Path -Path $dirPath -ChildPath $lowerCaseName
        try {
            Rename-Item -Path $file.FullName -NewName $tempName -ErrorAction Stop
            Rename-Item -Path $tempPath -NewName $lowerCaseName -ErrorAction Stop
            Write-Host "✓ 文件重命名: $originalName -> $lowerCaseName" -ForegroundColor Cyan
            $renamedFiles++
        }
        catch {
            Write-Warning "× 文件重命名失败 '$originalName': $($_.Exception.Message)"
        }
    }
}
# ==========================
# 2. 处理文件夹(从子目录向上)
# ==========================
$folders = Get-ChildItem -Path $FolderPath -Directory -Recurse | Sort-Object FullName -Descending
$totalFolders = $folders.Count
$renamedFolders = 0
foreach ($folder in $folders) {
    $parentPath = Split-Path $folder.FullName -Parent
    $originalName = $folder.Name
    $lowerCaseName = $originalName.ToLower()
    if ($originalName -cne $lowerCaseName) {
        $tempName = ([guid]::NewGuid().ToString() + ".tmp")
        $tempPath = Join-Path -Path $parentPath -ChildPath $tempName
        $targetPath = Join-Path -Path $parentPath -ChildPath $lowerCaseName
        try {
            Rename-Item -Path $folder.FullName -NewName $tempName -ErrorAction Stop
            Rename-Item -Path $tempPath -NewName $lowerCaseName -ErrorAction Stop
            Write-Host "✓ 文件夹重命名: $originalName -> $lowerCaseName" -ForegroundColor Green
            $renamedFolders++
        }
        catch {
            Write-Warning "× 文件夹重命名失败 '$originalName': $($_.Exception.Message)"
        }
    }
}
# ==========================
# 3. 最后处理最顶层文件夹本身(可选)
# ==========================
$baseFolderName = Split-Path -Path $FolderPath -Leaf
$baseParent = Split-Path -Path $FolderPath -Parent
$lowerCaseBase = $baseFolderName.ToLower()
if ($baseFolderName -cne $lowerCaseBase) {
    $tempName = ([guid]::NewGuid().ToString() + ".tmp")
    $tempPath = Join-Path -Path $baseParent -ChildPath $tempName
    $targetPath = Join-Path -Path $baseParent -ChildPath $lowerCaseBase
    try {
        Rename-Item -Path $FolderPath -NewName $tempName -ErrorAction Stop
        Rename-Item -Path $tempPath -NewName $lowerCaseBase -ErrorAction Stop
        Write-Host "✓ 顶层文件夹重命名: $baseFolderName -> $lowerCaseBase" -ForegroundColor Magenta
    }
    catch {
        Write-Warning "× 顶层文件夹重命名失败 '$baseFolderName': $($_.Exception.Message)"
    }
}
# ==========================
# 4. 汇总
# ==========================
Write-Host "`n全部处理完成!" -ForegroundColor Green
Write-Host "总文件数: $totalFiles, 重命名文件数: $renamedFiles" -ForegroundColor Cyan
Write-Host "总文件夹数: $totalFolders, 重命名文件夹数: $renamedFolders" -ForegroundColor Cyan
Read-Host "`n按回车键退出"