Get the App
SLTechnology News&Howtos  ›  Servers  › 

Powershell batch renaming file contains [] causing an error

Shulou Source: shulou.com Published: 2022-06-03 09:09:39 12月06日 Update

All downloaded files contain "[download website address]", according to the usual method.

Get-ChildItem "D:\ Bluey\"-Recurse | ForEach-Object {Rename-Item-Path $_ .FullName-NewName $_ .FullName.Replace ('old','new')}

It has been reported that the source file could not be found. After consulting, I learned that when the file name contains special characters, the-LiteralPath parameter is required.

Get-ChildItem "D:\ Bluey\"-Recurse | Where-Object {$_ .Name-match'\ [. +\]'} | foreach {Rename-Item-LiteralPath $_ .FullName-NewName $($_ .Name-replace'\ [. +\]','-')}

Replace [] and the characters between them with -.

Tags: File character special download site parameter address general report file name method source file website package Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology OPPO Reno Shulou Tech Info Huawei Redmi