Powershell:为什么 Rename-Item 不能用作管道命令?
Powershell: Why does Rename-Item not work as a piped command?
在这个脚本中,大部分都按我预期的那样工作。但是,重命名操作只会在这些管道命令之外起作用
Get-ChildItem -Path $folderpath -Filter $folderfile | Move-Item -
Destination $destination | sleep 5 | Out-File -FilePath $logpath -Append
如果我尝试将重命名作为管道命令的一部分进行,那根本行不通。除此之外的任何地方,它都将适用于文件观察器的单次迭代,然后就不再适用了。为什么重命名不能用作管道命令?
Get-ChildItem -Path $folderpath -Filter $folderfile | Move-Item -Destination $destination | Rename-Item $destination$folderfile -NewName $newname | Out-File -FilePath $logpath -Append
Move-Item
默认不输出到管道。使用 -PassThru 开关:
-PassThru
Returns an object representing the item with which you are working. By default, this cmdlet does not generate any output.
这会将其直接通过管道传输到 Rename-Item
,您只需指定 -NewName
:
Get-ChildItem -Path $folderpath -Filter $folderfile |
Move-Item -Destination $destination -PassThru |
Rename-Item -NewName $newname -PassThru |
Out-File -FilePath $logpath -Append
还有,你根本不用Rename-Item
直接移动到最终目标目录+名称(假设$destination
是目录路径):
Get-ChildItem -Path $folderpath -Filter $folderfile |
Move-Item -Destination (Join-Path $destination $newname) -PassThru |
Out-File -FilePath $logpath -Append
在这个脚本中,大部分都按我预期的那样工作。但是,重命名操作只会在这些管道命令之外起作用
Get-ChildItem -Path $folderpath -Filter $folderfile | Move-Item -
Destination $destination | sleep 5 | Out-File -FilePath $logpath -Append
如果我尝试将重命名作为管道命令的一部分进行,那根本行不通。除此之外的任何地方,它都将适用于文件观察器的单次迭代,然后就不再适用了。为什么重命名不能用作管道命令?
Get-ChildItem -Path $folderpath -Filter $folderfile | Move-Item -Destination $destination | Rename-Item $destination$folderfile -NewName $newname | Out-File -FilePath $logpath -Append
Move-Item
默认不输出到管道。使用 -PassThru 开关:
-PassThru
Returns an object representing the item with which you are working. By default, this cmdlet does not generate any output.
这会将其直接通过管道传输到 Rename-Item
,您只需指定 -NewName
:
Get-ChildItem -Path $folderpath -Filter $folderfile |
Move-Item -Destination $destination -PassThru |
Rename-Item -NewName $newname -PassThru |
Out-File -FilePath $logpath -Append
还有,你根本不用Rename-Item
直接移动到最终目标目录+名称(假设$destination
是目录路径):
Get-ChildItem -Path $folderpath -Filter $folderfile |
Move-Item -Destination (Join-Path $destination $newname) -PassThru |
Out-File -FilePath $logpath -Append