2017-06-28 79 views
1

我試圖在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 
    } 
} 

回答

2
Copy-Item -Path (Join-Path -Path $_.FullName -ChildPath '\*.txt') 

我建議你也可以使用-WhatIf在也來檢查它你想到的第一件事。

+0

謝謝,根據假設,它現在正在執行我想要的代碼,但沒有在文件夾中彈出任何內容。任何想法,爲什麼? –

+0

您是否刪除了-whatif? –

1

$_$PSItem(PowerShell 3.0+),訪問管道中的當前對象。從這裏,您可以訪問其成員。

+0

謝謝,根據假設,它現在正在執行我想要的代碼,但沒有在文件夾中彈出任何內容。任何想法,爲什麼? –

相關問題