名前文字列内の各単語の最初の文字を大文字に置き換えてディレクトリ名を変更するにはどうすればよいですか?

名前文字列内の各単語の最初の文字を大文字に置き換えてディレクトリ名を変更するにはどうすればよいですか?

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
#>

WhatIf/Confirmについて

答え2

このKernel32方法を使用するMoveFileと、名前を 2 回変更する必要なく、ファイルとディレクトリの名前を変更できます。

次のコードは、新しい名前のそれぞれのプロパティを使用してファイルとディレクトリの名前を変更します (タイプによって異なります)。

メソッド「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)`""
        }
    }
}

関連情報