Powershell Copy-Item 复制无法复制到叶上的 UNC 子目录的内容

Powershell Copy-Item copies contents of UNC subdirectory which cannot copy onto leaf

对于我重新定义此问题的以下术语可能引起的混淆,提前致歉;


问题

我正在尝试使用 Windows 7 Powershell 3.0 Copy-Item 功能将一个目录的六个子目录复制到另一个目录中;相当简单的东西。请注意,这些目录驻留在网络上,其 UNC 路径已映射到 windows 环境中的驱动器; H:\ = \servername\path

在下面的示例中,顶级目录本身被复制,而不仅仅是其内容(恰好是六个子目录及其内容)

$from = "H:\temp\toplvl_dir - backup"

$to = "H:\temp\newdir"

Copy-Item $from -Destination $to -Recurse

# Creates:
# H:\temp\newdir\toplvl_dir - backup

这是不可取的。 在使用“*”后缀 $from = "H:\temp\toplvl_dir\* " 借用 David-Tchepak solution 的格式方面取得了进展,这在一定程度上起作用,因为它只复制了内容...

$from = "H:\temp\toplvl_dir - backup\*"

$to = "H:\temp\newdir"

Copy-Item $from -Destination $to -Recurse

# Creates:
# H:\temp\newdir\{copied contents of .\toplvl_dir - backup\NN_subdir\}

...但不幸的是,这些内容源自低于所需级别的一级:即子目录的内容.\toplvl_dir - backup\NN_subdir\,而不是目录的内容。\toplvl_dir... 上例中的 $from 目录实际上包含六个子目录 "_subdir", "_subdir"...."_subdir",每个子目录包含一个相同的 'tree',由一个单独的子目录组成,该子目录本身包含许多文件。完整路径如下所示;

e.g. H:\temp\toplvl_dir - backup\NN_subdir\lone_subdirectory\{lots of files}

Copy-Item 似乎试图复制 的 sub_subdirectorylone_subdirectory,每个 NN_subdir 都报告一个错误; "container cannot be copied onto existing leaf item"。到目前为止,我了解到 NN_subdir 的内容在第一个 01_subdir 之后的每次复制迭代都是 lone_subdirectory 的重复,这将导致目录名称冲突,但我不理解为什么应用语法后,Copy-Item 试图复制 sub_subdirectories 而不是 子目录 .\toplvl_dir - backup\NN_subdir\

如何简单调用Copy-Item将顶级目录.\toplvl_dir - backup\*内容复制到.\newdir即六个子目录本身 它们的内容!?

如果我期望能够在对 Copy-Item?

的单行调用中完成此操作是不合理的,请随意说
# Desired Result using a one-line call to Copy-Item:
H:\temp\newdir_subdir\lone_subdirectory\{lots of files}
H:\temp\newdir_subdir\lone_subdirectory\{lots of files}
H:\temp\newdir_subdir\lone_subdirectory\{lots of files}
H:\temp\newdir_subdir\lone_subdirectory\{lots of files}
H:\temp\newdir_subdir\lone_subdirectory\{lots of files}
H:\temp\newdir_subdir\lone_subdirectory\{lots of files}

:-S

澄清一下,如果我有这样的文件夹结构:

C:\src\lone\a.txt
C:\src\lone\b.txt
C:\dest

您正在寻找一个 1 行 Copy-Item 命令来生成如下所示的目录结构?

C:\dest\lone\a.txt
C:\dest\lone\b.txt

对吗?如果是这样,这个命令对我有用(尽管是 Win7,PS 版本 5):

Copy-Item -Path 'C:\src\*' -Destination 'C:\dest' -Recurse