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
-
How do you want to handle filename collisions?Mathias R. Jessen– Mathias R. Jessen2017-09-13 14:47:46 +00:00Commented Sep 13, 2017 at 14:47
-
The destination directory is empty.Moe Hussa– Moe Hussa2017-09-13 14:48:45 +00:00Commented 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 .csvMoe Hussa– Moe Hussa2017-09-13 14:57:21 +00:00Commented Sep 13, 2017 at 14:57
Add a comment
|
1 Answer
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
}
}
3 Comments
Moe Hussa
Thank you @Esperento57. It was helpful but duplicate filenames were ignored. Is there a way to solve this?
Esperento57
Look my post for doublon ;)
Moe Hussa
Hey, Esperento57. It doesn't copy the files although I can see the -whatif trace. Thank you though man.