How can I bulk rename files in PowerShell?

前端 未结 8 2057
春和景丽
春和景丽 2021-01-30 10:36

I\'m trying to do the following:

Rename-Item c:\\misc\\*.xml *.tmp

I basically want to change the extension on every files within a directory t

8条回答
  •  感情败类
    2021-01-30 11:03

    The existing answers suggest the -replace operator, but what if the file is called a.xml.xml? Both .xml substrings will be replaced and the end result would be a.tmp.tmp. Fortunately, there's a .NET method for this:

    Dir *.xml | rename-item -newname { [io.path]::ChangeExtension($_.name, ".tmp") } 
    

    (Manish Kumar was close with GetFileNameWithoutExtension but this is more elegant and probably a bit more efficient, not that it overly matters in this case)

提交回复
热议问题