78

I'm trying to copy a file to a new location, maintaining directory structure.

$source = "c:\some\path\to\a\file.txt"
destination = "c:\a\more\different\path\to\the\file.txt"

Copy-Item  $source $destination -Force -Recurse

But I get a DirectoryNotFoundException:

Copy-Item : Could not find a part of the path 'c:\a\more\different\path\to\the\file.txt'
Yan Sklyarenko
  • 31,557
  • 24
  • 104
  • 139
Ev.
  • 7,109
  • 14
  • 53
  • 87

8 Answers8

152

The -recurse option only creates a destination folder structure if the source is a directory. When the source is a file, Copy-Item expects the destination to be a file or directory that already exists. Here are a couple ways you can work around that.

Option 1: Copy directories instead of files

$source = "c:\some\path\to\a\dir"; $destination = "c:\a\different\dir"
# No -force is required here, -recurse alone will do
Copy-Item $source $destination -Recurse

Option 2: 'Touch' the file first and then overwrite it

$source = "c:\some\path\to\a\file.txt"; $destination = "c:\a\different\file.txt"
# Create the folder structure and empty destination file, similar to
# the Unix 'touch' command
New-Item -ItemType File -Path $destination -Force
Copy-Item $source $destination -Force
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
ajk
  • 4,473
  • 2
  • 19
  • 24
  • 4
    Thanks Shay. The touch method is really handy no need to recursively go through the folders. – heedfull Sep 14 '12 at 13:10
  • 2
    Interesting, the "touch" creates the directory and file, and the file is a one line file.. if you open the file in notepad (no matter what the extension is) ...the file has the ~original name full name... as line1 of the (text?) file. Then the copy-item is very happy that all the folders exist correctly. THANKS! I have expanded your answer to be an answer of my question here: http://stackoverflow.com/questions/42818014/powershell-copy-files-with-a-blacklist-exclude-and-a-whitelist-include/42819089#42819089 – granadaCoder Mar 15 '17 at 21:41
20

Alternatively, with PS3.0 onwards, you can simply use the New-Item to create the target folder directly, without having to create a "dummy" file, e.g. ...

New-Item -Type dir \\target\1\2\3\4\5

...will happily create the \\target\1\2\3\4\5 structure irrespective of how much of it already exists.

Mike
  • 2,120
  • 1
  • 23
  • 40
12

Here's a oneliner to do this. Split-Path retrieves the parent folder, New-Item creates it and then Copy-Item copies the file. Please note that the destination file will have the same filename as the source file. Also, this won't work if you need to copy multiple files to the same folder as with the second file you'll get An item with the specified name <destination direcory name> already exists error.

Copy-Item $source -Destination (New-Item -Path (Split-Path -Path $destination) -Type Directory)
Lopez
  • 121
  • 2
  • 5
  • 7
    The problem with existing directory can be solved by passing `-Force` to the New-Item cmdlet. – Palec Jan 03 '20 at 21:41
4

I had files in a single folder in Windows 7 that I wanted to rename and copy to nonexistent folders.

I used the following PowerShell script, which defines a Copy-New-Item function as a wrapper for the Test-Item, New-Item, and Copy-Item cmdlets:

function Copy-New-Item {
  $SourceFilePath = $args[0]
  $DestinationFilePath = $args[1]

  If (-not (Test-Path $DestinationFilePath)) {
    New-Item -ItemType File -Path $DestinationFilePath -Force
  } 
  Copy-Item -Path $SourceFilePath -Destination $DestinationFilePath
}

Copy-New-Item schema_mml3_mathml3_rnc schema\mml3\mathml3.rnc
# More of the same...
Copy-New-Item schema_svg11_svg_animation_rnc schema\svg11\svg-animation.rnc
# More of the same...
Copy-New-Item schema_html5_assertions_sch schema\html5\assertions.sch
# More of the same...

(Note that, in this case, the source file names have no file extension.)

If the destination file path does not exist, the function creates an empty file in that path, forcing the creation of any nonexistent directories in the file path. (If Copy-Item can do all that by itself, I could not see how to do it from the documentation.)

Graham Hannington
  • 1,749
  • 16
  • 18
3

It is coming late, but as I stumbled upon this question looking for a solution to a similar problem, the cleanest one I found elsewhere is using robocopy instead of Copy-Item. I needed to copy the whole file structure together with the files, that's easily achieved via

robocopy "sourcefolder" "destinationfolder" "file.txt" /s 

Detail about robocopy: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy

crankedrelic
  • 463
  • 1
  • 5
  • 14
  • I like this one because it gives back a full report of the copy process if you capture it in a variable. `$result = robocopy.exe "sourcefolder" "destinationfolder" "file.txt" /s` – Fütemire Oct 06 '21 at 22:28
1

None of the current answers worked for me to fix the Could not find a part of the path error raised by Copy-Item. After some research and testing, I discovered this error can be raised if the Destination path goes over the 260 character Windows path length limit.

What I mean by that is: if you supply a path to the Destination argument of Copy-Item and any of the files you are copying would exceed the 260 character limit when copied to the Destination folder, Copy-Item will raise the Could not find a part of the path error.

The fix is to shorten your Destination path, or to shorten/flatten the folder structure in the source directory that you are trying to copy.

Lews Therin
  • 3,707
  • 2
  • 27
  • 53
0

May be Helpfull:

$source = 'c:\some\path\to\a\file.txt'
$dest = 'c:\a\more\different\path\to\the\file.txt'
$dest_dir = 'c:\a\more\different\path\to\the\'

[System.IO.Directory]::CreateDirectory($dest_dir);
if(-not [System.IO.File]::Exists($dest))
{
    [System.IO.File]::Copy($source,$dest);
}
Romylussone
  • 773
  • 1
  • 8
  • 19
-1

I have been digging around and found a lot of solutions to this issue, all being some alteration not just a straight copy-item command. Grant it some of these questions predate PS 3.0 so the answers are not wrong but using powershell 3.0 I was finally able to accomplish this using the -Container switch for copy-item.

Copy-Item $from $to -Recurse -Container

this was the test i ran, no errors and destination folder represented the same folder structure.

New-Item -ItemType dir -Name test_copy
New-Item -ItemType dir -Name test_copy\folder1
New-Item -ItemType file -Name test_copy\folder1\test.txt
#NOTE: with no \ at the end of the destination the file is created in the root of the destination, does not create the folder1 container
#Copy-Item D:\tmp\test_copy\* D:\tmp\test_copy2 -Recurse -Container

#if the destination does not exists this created the matching folder structure and file with no errors
Copy-Item D:\tmp\test_copy\* D:\tmp\test_copy2\ -Recurse -Container
workabyte
  • 3,496
  • 2
  • 27
  • 35
  • 3
    This is not [what **-Container** does](https://stackoverflow.com/a/129385/3273963). You have wrong reasoning for what's happening. -Recurse will cause destination folder creation. Difference is when "test_copy2" does not exist, content of test_copy will become (not be copied into) test_copy2. Try to make another folder in test_copy, then perform copy-item without creating test_copy2 and you'll end up with error. And it also doesn't matter whether you use "\" at the end, -Container is always $true and has a different function as assumed here. – papo Aug 22 '17 at 19:57
  • @papo i give no reason that it works just that it did for the example i provided – workabyte Nov 12 '19 at 01:03
  • But reasons are very important in commands. If you use a command without understanding how it works, it might work in one case, but fail in another. Obviously you made a mistake during testing. I suppose you did not remove destination test folder after first test. Did you consider my reaction and tested again? Do you understand that if -Container defaults to $true, (docs is wrong) it makes no difference if you use it or not. You can only switch it OFF by: `-Container:$false`. Try it. Don't just post assumptions on forum, people are learning from it. – papo Nov 12 '19 at 16:31