Rename files to lowercase in Powershell

允我心安 提交于 2019-12-02 20:33:04

Even though you have already posted your own answer, here is a variation:

dir Leaflets -r | % { if ($_.Name -cne $_.Name.ToLower()) { ren $_.FullName $_.Name.ToLower() } }

Some points:

  • dir is an alias for Get-ChildItem (and -r is short for -Recurse).
  • % is an alias for ForEach-Object.
  • -cne is a case-sensitive comparison. -ne ignores case differences.
  • $_ is how you reference the current item in the ForEach-Object loop.
  • ren is an alias for Rename-Item.
  • FullName is probably preferred as it ensures you will be touching the right file.

If you wanted to excludes directories from being renamed, you could include something like:

if ((! $_.IsPsContainer) -and $_.Name -cne $_.Name.ToLower()) { ... }

Hopefully this is helpful in continuing to learn and explore PowerShell.

Keep in mind that you can pipe directly to Rename-Item and use Scriptblocks with the -NewName parameter (because it also accepts pipeline input) to simplify this task:

Get-ChildItem -r | Where {!$_.PSIsContainer} | 
                   Rename-Item -NewName {$_.FullName.ToLower()}

and with aliases:

gci -r | ?{!$_.PSIsContainer} | rni -New {$_.FullName.ToLower()}

slight tweak on this, if you only want to update the names of files of a particular type try this:

get-childitem *.jpg | foreach { if ($_.Name -cne $_.Name.ToLower()) { ren $_.FullName $_.Name.ToLower() } }

this will only lowercase the jpg files within your folder and ignore the rest

You need to temporarily rename them to something else then name them back all lower case.

$items = get-childitem -Directory -Recurse

foreach ($item in $items)
{

   if ($item.name -eq $item.name.ToLower())
   {    

       $temp = $item.FullName.ToLower() + "_"
       $name = $item.FullName.ToLower()
       ren $name $temp

       ren $temp $name
   }

There are many issues with the previous given answers due to the nature of how Rename-Item, Piping, Looping and the Windows Filesystem works. Unfortunatly the the most simple (not using aliases for readability here) solution I found to rename all files and folders inside of a given folder to lower-case is this one:

Get-ChildItem -Path "/Path/To/You/Folder" -Recurse | Where{ $_.Name -cne $_.Name.ToLower() } | ForEach-Object { $tn="$($_.Name)-temp"; $tfn="$($_.FullName)-temp"; $nn=$_.Name.ToLower(); Rename-Item -Path $_.FullName -NewName $tn; Rename-Item -Path $tfn -NewName $nn -Force; Write-Host "New Name: $($nn)";}

It's more idomatic in PowerShell to use where instead of if in a pipeline:

gci -Recurse Leaflets | 
    ? { $_.Name -ne $_.Name.ToLower()) } | 
    % { ren -NewName $_.Name.ToLower() }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!