2017-09-13 124 views
1

我很難用複製項目複製文件(只)遞歸與特定的擴展名(* .txt)沒有目錄結構。任何人都可以請給我一隻手嗎?我已經使用了帶有-recurse的gci並將輸出傳遞給copy-item,它會給出錯誤提示輸入錯誤。 Get-ChildItem -Path $source -include "*name*.txt" -recurse | Copy-Item $source $dest複製文件只有不使用PowerShell的目錄結構

+0

你如何想處理文件名衝突? –

+0

目標目錄爲空。 –

+0

我不擔心重複的文件名,我已經把完整的路徑和文件名放在.csv –

回答

0

使用-file僅用於文件而不用於目錄。複製項目進入管道必須有唯一的目的地路徑... -force如果doublon文件

試試這個:

gci $source -file -include "*name*.txt" -recurse | copy-item -Destination $dest -Force 

,如果你有doublon你可以試試這個:

$source="C:\temp\Release" 
$dest="c:\temp\x" 
gci $source -file -include "*name*.txt" -recurse | group Name | %{ 

    for ($i = 0; $i -lt $_.Count; $i++) 
    { 
    if ($_.Count -gt 1) 
    { 
     $destination="{0}\{1}_{2}" -f $dest, $i, $_.Group[$i].Name 
    } 
    else 
    { 
     $destination="{0}\{1}" -f $dest, $_.Group[$i].Name 
    } 

    copy-item $_.Group[$i].FullName $destination -WhatIf -Force 

    } 

} 
+0

謝謝@ Esperento57。這很有幫助,但重複的文件名被忽略。有沒有辦法解決這個問題? –

+0

看我的帖子爲doublelon;) – Esperento57

+0

嘿,Esperento57。它不會複製這些文件,儘管我可以看到-whatif跟蹤。謝謝你,雖然人。 –