이름 문자열에 있는 각 단어의 첫 글자를 대문자로 바꾸어 디렉터리 이름을 어떻게 바꿀 수 있습니까?

이름 문자열에 있는 각 단어의 첫 글자를 대문자로 바꾸어 디렉터리 이름을 어떻게 바꿀 수 있습니까?

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파일과 디렉터리의 이름을 두 번 바꿀 필요 없이 이름을 바꿀 수 있습니다 .

다음 코드는 새 이름에 대한 해당 속성을 사용하여(유형에 따라) 파일과 디렉터리의 이름을 바꿉니다.

'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)`""
        }
    }
}

관련 정보