4

I'm trying to use Copy-Item to copy a file to an existing folder and getting the error "Could not find part of path". I've Googled this problem for ages, but can't seem to find an answer.

I altered the code to check that I could copy to each folder in the path and then hit the problem when .'s were used in the file name. Also I cannot change the folder name because the folder name is created by a (crappy) Oracle related installer.

Essentially the code is like this (the code is run in the context of the source directory)...

$Filename = "new.txt"
$Destination = "C:\a\b\c\d\e.f\g\h"
Copy-Item -Path $Filename -Destination $Destination

So this works for folders a,b,c and d, but for folder e.f new.txt is copied to folder d and renamed to e.f (which makes sense) and for folders g and h I get the above error. How do I get Copy-Item to see e.f as a folder?

Cheers,

Matt

Joshua Drake
  • 2,704
  • 3
  • 35
  • 54
Matty W
  • 65
  • 1
  • 2
  • 8

4 Answers4

0

Create the directory first: New-Item -Path d:\a\b\c\d\e.f\g\h -type directory

-1

You can force Copy-Item to see the destination as a folder by appending a backslash:

Copy-Item -Path $Filename -Destination ($Destination + '\')
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • This does not work in this instance, the same error occurs. The fact that C:\a\b\c\d\e.f\g\h doesn't kind of indicates this as e.f does have a \ after it. Appending \ to the end of e.f, g or h makes no difference in my tests. – Matty W Jul 17 '13 at 11:13
  • 1
    Please provide your full code. `Copy-Item` to an existing destination ``C:\a\b\c\d\e.f\g\h\`` works as described. If it doesn't work for you you're doing something different from what you said. – Ansgar Wiechers Jul 17 '13 at 11:26
  • For all intents and purposes, the code is exactly the same except the problem folder actually has two .'s in it, so the path is more like C:\a\b\c\d\11.2.0\g\h – Matty W Jul 17 '13 at 11:43
  • 1
    Time to fess up apparently I can;t tell the difference between 1's and l's (they are kinda similar). – Matty W Jul 17 '13 at 13:32
  • So the issue was caused by a mistyped path and is now resolved? – Ansgar Wiechers Jul 17 '13 at 18:26
-1

What you have works fine. The only thing I had to do to make is work was remove the $ from the $copy-item line.

Kevin_
  • 2,916
  • 3
  • 19
  • 18
  • Well done for spotting the deliberate mistake! Unfortunately the mistake is only in this forum, my script that is failing fine $ wise. – Matty W Jul 17 '13 at 13:21
  • 1
    Time to fess up, the $ wasn't the issue. Apparently I can't tell the difference between 1's and l's (they are kinda similar). – Matty W Jul 17 '13 at 13:31
-1

Create the directory first. This is not an issue.

$Filename = "new.txt"
$Destination = "C:\a\b\c\d\e.f\g\h"
mkdir $Destination
Copy-Item -Path $Filename -Destination $Destination
js2010
  • 23,033
  • 6
  • 64
  • 66