Copy-Item 是否应该创建目标目录结构?

发布于 2024-12-05 23:41:22 字数 371 浏览 0 评论 0原文

我正在尝试将文件复制到新位置,维护目录结构。

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

Copy-Item  $source $destination -Force -Recurse

但我收到一个 DirectoryNotFoundException

Copy-Item : Could not find a part of the path 'c:\a\more\different\path\to\the\file.txt'

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

柳絮泡泡 2024-12-12 23:41:22

如果源是目录,则 -recurse 选项仅创建目标文件夹结构。当源是文件时,Copy-Item 期望目标是已存在的文件或目录。您可以通过以下几种方法来解决这个问题。

选项 1: 复制目录而不是文件

$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

选项 2: 先“触摸”文件,然后覆盖它

$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

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
想你只要分分秒秒 2024-12-12 23:41:22

或者,从 PS3.0 开始,您可以简单地使用 New-Item 直接创建目标文件夹,而不必创建“虚拟”文件,例如……

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

将愉快地创建 \\target\1\ 2\3\4\5 结构,无论其中有多少已经存在。

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.

七颜 2024-12-12 23:41:22

这是一个用于执行此操作的单行代码。 Split-Path 检索父文件夹,New-Item 创建它,然后 Copy-Item 复制文件。请注意,目标文件将与源文件具有相同的文件名。此外,如果您需要将多个文件复制到与第二个文件相同的文件夹,则这将不起作用,您将获得 An item with the specified name;已存在 错误。

Copy-Item $source -Destination (New-Item -Path (Split-Path -Path $destination) -Type Directory)

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)
山色无中 2024-12-12 23:41:22

来晚了,但当我偶然发现这个问题寻找类似问题的解决方案时,我在其他地方找到的最干净的方法是使用 robocopy 而不是 Copy-Item。我需要将整个文件结构与文件一起复制,这可以通过

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

有关 robocopy 的详细信息轻松实现: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy

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

醉城メ夜风 2024-12-12 23:41:22

我在 Windows 7 的单个文件夹中拥有一些文件,我想将其重命名并复制到不存在的文件夹中。

我使用了以下 PowerShell 脚本,该脚本将 Copy-New-Item 函数定义为 Test-ItemNew-Item、和 Copy-Item cmdlet:(

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...

请注意,在这种情况下,源文件名没有文件扩展名。)

如果目标文件路径不存在,该函数将在该路径中创建一个空文件,强制创建文件中任何不存在的目录 小路。 (如果Copy-Item可以自己完成所有这些,我无法从文档。)

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.)

听不够的曲调 2024-12-12 23:41:22

当前的答案都无法帮助我修复由 Copy-Item 引发的 Could not find a part of the path 错误。经过一些研究和测试,我发现如果 Destination 路径经过 260 个字符的 Windows 路径长度限制

我的意思是:如果您提供 Copy-ItemDestination 参数的路径,并且您正在复制的任何文件在复制时都会超出 260 个字符的限制复制到 Destination 文件夹时,Copy-Item 将引发 无法找到路径的一部分 错误。

修复方法是缩短您的Destination 路径,或者缩短/展平您尝试复制的源目录中的文件夹结构。

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.

请恋爱 2024-12-12 23:41:22

可能有帮助:

$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);
}

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);
}
朕就是辣么酷 2024-12-12 23:41:22

我一直在挖掘并找到了很多解决这个问题的方法,所有这些都是一些改变而不仅仅是直接的复制项目命令。承认其中一些问题早于 PS 3.0,所以答案没有错误,但使用 powershell 3.0,我终于能够使用复制项的 -Container 开关来完成此任务。

Copy-Item $from $to -Recurse -Container

这是我运行的测试,没有错误,并且目标文件夹代表相同的文件夹结构。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文