copy files only not directory structure using powershell

1.2k views Asked by At

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

1

There are 1 answers

3
Esperento57 On

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 

  }

}