简体   繁体   中英

copy files only not directory structure using powershell

I am having a hard time using Copy-item for copying files(only) recursively with a particular extension(*.txt) without the directory structure. Can anyone please give me a hand? I have used the the gci with -recurse and piped the output to copy-item, it gives an error saying improper input. Get-ChildItem -Path $source -include "*name*.txt" -recurse | Copy-Item $source $dest

Use -file for take only file and not directory. Copy-item into pipe must have only destination path... -force if doublon file

try this:

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

if you have doublon you can try this :

$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 

  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM