Powershell:为什么重命名项不能作为管道命令使用?
在这个脚本中,一切都按照我的预期进行。但是,重命名操作只能在这些管道命令之外工作Powershell:为什么重命名项不能作为管道命令使用?,powershell,powershell-2.0,pipeline,Powershell,Powershell 2.0,Pipeline,在这个脚本中,一切都按照我的预期进行。但是,重命名操作只能在这些管道命令之外工作 Get-ChildItem -Path $folderpath -Filter $folderfile | Move-Item - Destination $destination | sleep 5 | Out-File -FilePath $logpath -Append 如果我尝试将重命名作为管道命令的一部分,它根本不起作用。除此之外的任何地方,它都可以在filewatcher的单个迭代中工作,然后就
Get-ChildItem -Path $folderpath -Filter $folderfile | Move-Item -
Destination $destination | sleep 5 | Out-File -FilePath $logpath -Append
如果我尝试将重命名作为管道命令的一部分,它根本不起作用。除此之外的任何地方,它都可以在filewatcher的单个迭代中工作,然后就不再工作了。为什么重命名不能作为管道命令使用
Get-ChildItem -Path $folderpath -Filter $folderfile | Move-Item -Destination $destination | Rename-Item $destination$folderfile -NewName $newname | Out-File -FilePath $logpath -Append
Move Item
默认情况下不会输出到管道。使用-PassThru开关:
-直通返回表示正在使用的项的对象。默认情况下,此cmdlet不生成任何输出 这将直接将其导入
重命名项
,您只需指定-NewName
:
Get-ChildItem -Path $folderpath -Filter $folderfile |
Move-Item -Destination $destination -PassThru |
Rename-Item -NewName $newname -PassThru |
Out-File -FilePath $logpath -Append
此外,您甚至不必使用重命名项
,而是直接将其移动到最终的目标目录+名称(假设$destination
是一个目录路径):
你能把代码缩减到只有相关部分吗?当然可以。没问题。谢谢你看!
Get-ChildItem -Path $folderpath -Filter $folderfile |
Move-Item -Destination (Join-Path $destination $newname) -PassThru |
Out-File -FilePath $logpath -Append