1

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

3
  • How do you want to handle filename collisions? Commented Sep 13, 2017 at 14:47
  • The destination directory is empty. Commented Sep 13, 2017 at 14:48
  • I am not worried about the repeated filenames as well, I had already put the full path and filename in a .csv Commented Sep 13, 2017 at 14:57

1 Answer 1

0

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 

  }

}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @Esperento57. It was helpful but duplicate filenames were ignored. Is there a way to solve this?
Look my post for doublon ;)
Hey, Esperento57. It doesn't copy the files although I can see the -whatif trace. Thank you though man.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.