문제

I want to change the names of some files automatically.

With this code I change the lowercase letters to uppercase:

get-childitem *.mp3 | foreach { if ($.Name -cne $.Name.ToUpper()) { ren $.FullName $.Name.ToUpper() } }

But I only want the first letter of each word to be uppercase.

도움이 되었습니까?

해결책

You can use ToTitleCase Method:

$TextInfo = (Get-Culture).TextInfo
$TextInfo.ToTitleCase("one two three")

outputs

One Two Three

$TextInfo = (Get-Culture).TextInfo
get-childitem *.mp3 | foreach { $NewName = $TextInfo.ToTitleCase($_); ren $_.FullName $NewName }

다른 팁

My answer is very similar, but I wanted to provide a one-line solution. This also forces the text to lowercase before forcing title case. (otherwise, only the first letter is effected)

$text = 'one TWO thrEE'
( Get-Culture ).TextInfo.ToTitleCase( $text.ToLower() )

Output:

One Two Three

Yup, it's built into Get-Culture.

gci *.mp3|%{
    $NewName = (Get-Culture).TextInfo.ToTitleCase($_.Name)
    $NewFullName = join-path $_.directory -child $NewName
    $_.MoveTo($NewFullName)
}

Yeah, it could be shortened to one line, but it gets really long and is harder to read.

There you go

[cultureinfo]::GetCultureInfo("en-US").TextInfo.ToTitleCase("what is my name?")
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top