使用foreach循环在powershell中批量重命名文件
您好,我试图重命名文件夹下的文件使用上述命令,但无法这样做,并得到以下错误使用foreach循环在powershell中批量重命名文件,powershell,Powershell,您好,我试图重命名文件夹下的文件使用上述命令,但无法这样做,并得到以下错误 $folderpath = 'E:\BOOKS\Python\python\python' $items = Get-ChildItem -Recurse $folderpathc *_pdf foreach( $i in $items) { Rename-Item E:\BOOKS\Python\python\python\$i E:\BOOKS\Python\python\python\$i.pdf } Renam
$folderpath = 'E:\BOOKS\Python\python\python'
$items = Get-ChildItem -Recurse $folderpathc *_pdf
foreach( $i in $items) { Rename-Item E:\BOOKS\Python\python\python\$i E:\BOOKS\Python\python\python\$i.pdf }
Rename Item:无法重命名,因为“E:\BOOKS\Python\Python\Python\book\u pdf”中的项不存在。
第1行字符:37
+foreach($i in$items){Rename Item您把事情复杂化了。不要重新键入文件的路径名,使用Get-ChildItem cmdlet已经提供的FullName属性。然后只使用BaseName属性的子字符串删除最后4个字符,并在末尾添加“.pdf”
Rename-Item : Cannot rename because item at 'E:\BOOKS\Python\python\python\book_pdf' does not exist.
At line:1 char:37
+ foreach( $i in $items) { Rename-Item <<<< E:\BOOKS\Python\python\python\$i E:\BOOKS\Python\python\python\$i.pdf }
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
看起来您想将所有“\u pdf”更改为“.pdf”,如果是这样,这是一种非常简单的方法
$folderpath = 'E:\BOOKS\Python\python\python'
$items = Get-ChildItem -Recurse $folderpathc *_pdf
foreach( $i in $items) {
Rename-Item $i.FullName ($i.basename.substring(0,$i.BaseName.length-4)+".pdf")
}
可能重复的
ls -Path 'E:\BOOKS\Python\python\python' -Filter *_pdf |
ForEach-Object {$_ | Rename-Item -NewName $_.Name.Replace('_pdf', '.pdf')}