我試圖在for-eachobject
循環中找到當前正在運行的對象的路徑。有人可以解釋如何做到這一點?我正在嘗試將Copy-Item
的源路徑設置爲文件所在的路徑。這是我與Powershell合作的第一個項目。假設$ Src已被正確實例化。如何在for-eachobject循環中查找當前對象的路徑
Get-ChildItem -Path $Src -Recurse -Force | Where-Object {$_.Name -notmatch "Archive" -and ($_.PSIsContainer)}|
ForEach-Object {
$DateStr = $_.BaseName.Substring(0,2)+'/'+$_.BaseName.Substring(3,2)+'/'+$_.BaseName.Substring(6,4)
$FileDate = get-date $DateStr
If ($FileDate -ge $Date -and !($_.PSIsContainer)) {
$ParentOjbect = $_.Directory
$Parent = $ParentOjbect.Name
Copy-Item -Path (Join-Path -Path $Src -ChildPath '\*.txt') #this
#needs to be corrected to only point to the current file
-Dest (Join-Path $Dst ("$($_.Directory.Name) $($_.Name)")) -WhatIf
}
}
謝謝,根據假設,它現在正在執行我想要的代碼,但沒有在文件夾中彈出任何內容。任何想法,爲什麼? –
您是否刪除了-whatif? –