Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用foreach循环在powershell中批量重命名文件_Powershell - Fatal编程技术网

使用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')}