如何重新命名目錄名稱,將名稱字串中每個單字的第一個字母替換為大寫?

如何重新命名目錄名稱,將名稱字串中每個單字的第一個字母替換為大寫?

在 Windows 10 電腦上,我一直在使用 PFrank 工具和 Power Toys PowerRenamer 等免費實用程式。使用 PFrank,我可以將檔案名稱中每個單字的大小寫更改為大寫,但它不會更改目錄名稱。

請問您知道 Powershell、CMD 或 BASH(使用 Windows 子系統 linux)中的命令或腳本可以實現這一點嗎?或可以做到這一點的工具(最好是開源的)。由於我已經更改了文件名,因此我只想僅對目錄名稱遞歸地執行此操作。

謝謝。

答案1

從我的評論延伸。就是這樣。

更新

刪除了原始的長答案並將其替換為以下內容以解決您的評論:

(Get-ChildItem -Path 'D:\Temp' -Directory -Recurse) -match 'src|dest' | 
ForEach-Object {
    "Proccessing $($PSItem.FullName) to TitleCase"
    $renameItemTempSplat = @{
        Path     = $PSitem.FullName
        NewName  = "$((Get-Culture).Textinfo.ToTitleCase($PSitem.Name.ToLower()))1"
        #WhatIf   = $true 
    }
    Rename-Item @renameItemTempSplat -PassThru | 
    Rename-Item -NewName $($((Get-Culture).Textinfo.ToTitleCase($PSitem.Name.ToLower())) -replace '1')
}
(Get-ChildItem -Path 'D:\Temp' -Directory -Recurse) -match 'src|dest'
# Results
<#
Proccessing D:\Temp\dest to TitleCase
Proccessing D:\Temp\Destination to TitleCase
Proccessing D:\Temp\src to TitleCase


    Directory: D:\Temp


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         12-Oct-20     14:27                Dest
d-----         24-Jan-21     22:24                Destination
d-----         12-Oct-20     13:27                Src
#>

關於假設/確認

答案2

您可以使用該Kernel32方法MoveFile重新命名檔案和目錄,而無需重新命名兩次。

以下程式碼使用新名稱的對應屬性(取決於類型)重新命名檔案和目錄。

「MoveFile」方法實際上會觸發與使用 Windows 資源管理器互動執行的過程相同的過程。

# "MoveFile" works for files and folders

# only add type once
if ($null -eq ('Win32.Kernel32' -as [type])) {
    # add method 'MoveFile from' kernel32.dll
    # https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-movefile
    $signature = @'
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool MoveFile(string lpExistingFileName, string lpNewFileName);
'@
    Add-Type -MemberDefinition $signature -Name 'Kernel32' -Namespace Win32
}

$dirPath = 'C:\temp\CaseTest'
Get-ChildItem -LiteralPath $dirPath -Recurse -Directory | ForEach-Object {
    $parentPath = $currentName = $fileExtension = $null

    if ($_ -is [System.IO.DirectoryInfo]) {
        # Directories
        $parentPath  = $_.Parent.FullName
        $currentName = $_.Name

    } elseif ($_ -is [System.IO.FileInfo]) {
        # Files
        $parentPath    = $_.Directory.FullName
        $currentName   = $_.BaseName
        $fileExtension = $_.Extension
    }

    if ($null -notin $currentName, $parentPath) {
        $newName =  @(
            [cultureinfo]::CurrentCulture.TextInfo.ToTitleCase($currentName.ToLower()),
            $fileExtension
        ) -join ''

        $newPath    = Join-Path -Path $parentPath -ChildPath $newName
        $moveResult = [Win32.Kernel32]::MoveFile($_.FullName, $newPath)

        if (-not $moveResult) {
            # 'MoveFile' returns only $false in case of errors, 
            # so we have to build the respective exception.
            # This requires "SetLastError = true" in signature
            $win32Error     = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
            $win32Exception = [System.ComponentModel.Win32Exception]$win32Error
            Write-Error -Exception $win32Exception `
                -Message "$($win32Exception.Message) `"$($_.FullName)`""
        }
    }
}

相關內容