Powershell Copy Recursive创建n Data文件夹

时间:2015-02-10 07:50:04

标签: powershell recursion copy

$from = ".\test\Application Data\software1\config\1.0"
$to = ".\test\Profile2k8\Appdata"

$exclude = @("new.ini","new2.ini")

Get-ChildItem $from -Recurse -Exclude $exclude | Copy-Item -Destination {Join-Path $to $_.FullName.Substring($from.length)}

当我运行此脚本时,它将复制文件,但它还在目标路径中包含一个n数据文件夹,如下所示:

.\test\Profile2k8\Appdata\n Data\software1\config\1.0

为什么要创建此文件夹,如何防止这种情况?

非常感谢

2 个答案:

答案 0 :(得分:0)

会改变$_.FullName.Substring($from.length) 只是$_.Name正如预期的那样工作?

编辑:哦,对,现在注意到了递归位,这可能会改变一些事情。

答案 1 :(得分:0)

最后更改你的代码行,如下所示,我希望它可以用于:

Get-ChildItem $from -Recurse -Exclude $exclude | Copy-Item -Destination {Join-Path $to $_.FullName.Substring((Convert-Path $from).length)}

您的代码正在尝试获取字符串$from的长度(其中只包含相对路径而不是完整路径),但实际路径长度更像是

"C:\test\Application Data\software1\config\1.0"