1

I have attempted to find the answer on this site, but haven't been successful.

I have 243 .png files that I am wanting to mass rename because, obviously, it would be tedious to do one by one. I haven't tried any command or script because I don't know how to even start after searching on google. I know how to use PowerShell - just not writing my own command/script. Okay, with that being said, here's what I'm talking about.

Currently the files are:

afghanistan flag.png
aland islands flag.png
peru flag.png

They need to be changed to:

Afghanistan Flag.png
Aland Islands Flag.png
Peru Flag.png

Thank you very much.

Dilan
  • 2,610
  • 7
  • 23
  • 33
Sanders
  • 30
  • 1
  • 10
  • Welcome to stackoverflow. Your question has been closed as a duplicate of another question. Please look at that question to get a solution. I would personally look at `ToTitleCase` as in the accepted answer to that question. – Palle Due Apr 16 '20 at 06:42
  • Thank you very much for this. I have no idea how I missed that - I believe I was just searching incorrectly. I looked at that accepted answer, and it worked perfectly. – Sanders Apr 16 '20 at 08:49

2 Answers2

1

Enter the directory by using a cd command: cd MYPATH

$names = (ls).name
foreach ($name in $names) {
    $name_split = $name.split(" ")
    $old_name = $name
    $name = ""
        foreach ($name_part in $name_split) {
            $name_part = $name_part.substring(0,1).toupper()+$name_part.substring(1).tolower()    
            $name += ($name_part + " ")
        }
    Move-Item -Path $old_name -Destination $name.substring(0,$name.length - 1)
}

This should work for you. Good on you for thinking some boring task could be automated with powershell. Let me know how it goes!

Bennett Forkner
  • 336
  • 1
  • 5
  • 1
    This one is perfect because it capitalizes the first letter of the file names, but it also keeps the file extension (.png) in lowercase. So, while there are others that mass rename files - this script does what I needed verbatim! Thank you for everything. – Sanders Apr 16 '20 at 09:00
0

Something like this should work:

$files = @('afghanistan flag.png'
 'aland islands flag.png'
 'peru flag.png')

$files | ForEach-Object {
  $parts = $_ -split " " | ForEach-Object { 
  "$($_.substring(0,1).toupper())$($_.substring(1).tolower())" }
  $newfilename = $parts -join ' '
  Write-Host "This is the new name: $newfilename"
}

Output:

This is the new name: Afghanistan Flag.png
This is the new name: Aland Islands Flag.png
This is the new name: Peru Flag.png
michiel Thai
  • 547
  • 5
  • 16