سؤال

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