【发布时间】:2017-12-04 09:50:33
【问题描述】:
对于我重新定义此问题的以下术语可能引起的混淆,提前道歉;
- 目录(顶级目录)
- 子目录(顶级目录下的目录)
- sub_subdirectory(子目录的子目录)
- 目录内容(文件和子目录)
- 子目录的内容(文件和子子目录)
问题
我正在尝试使用 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 目录实际上包含六个子目录"\01_subdir", "\02_subdir"...."\06_subdir",每个子目录都包含一个相同的“树”,该“树”由一个单独的子目录组成,该子目录本身包含大量文件。完整路径如下所示;
e.g. H:\temp\toplvl_dir - backup\NN_subdir\lone_subdirectory\{lots of files}
Copy-Item 似乎试图复制 sub_subdirectory 即lone_subdirectory,为每个NN_subdir 报告错误; “无法将容器复制到现有的叶项”。到目前为止,我了解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\01_subdir\lone_subdirectory\{lots of files}
H:\temp\newdir\02_subdir\lone_subdirectory\{lots of files}
H:\temp\newdir\03_subdir\lone_subdirectory\{lots of files}
H:\temp\newdir\04_subdir\lone_subdirectory\{lots of files}
H:\temp\newdir\05_subdir\lone_subdirectory\{lots of files}
H:\temp\newdir\06_subdir\lone_subdirectory\{lots of files}
:-S
【问题讨论】:
-
你能在没有通配符的情况下运行相同的命令吗?
$from = "H:\temp\toplvl_dir - backup\"