2017-09-13 46 views
1

我想创建一个数组,将从目标路径中删除文件,然后从源路径复制到目标路径。我已经在构建服务器上创建了一个.txt文件,并带有相对路径的文件列表。当我运行的代码,它移除文件夹B中的所有内容,下面的框和复制文件夹(没有任何内容)到文件夹B.找出使用powershell阵列

这就是我正在

$files = get-content "C:\files.txt" 
foreach ($filepath in $files) 
{ 
    $source = '\\Server1\Folder A' + $filepath 
    $dest = '\\Server2\Folder B' + $filepath 
    foreach ($removefile in $dest) 
    { 
     rd $removefile -recurse -force 
    } 
    foreach ($addfile in $source) 
    { 
     cp $addfile -destination $dest 
    } 
} 

纯碱,

我试过你的建议,但它试图从/复制到不正确的目录。

代码:

$targetList = Get-Content "C:\MCSfiles.txt" 

foreach ($target in $targetList) { 

    $destPath = Join-Path "\\Server2\MCSWebTest" $target 
    $destFiles = Get-ChildItem $destPath 

    foreach ($file in $destFiles) { 
     Remove-Item $file -Recurse -Force 
    } 

    $sourcePath = Join-Path "\\Server1\WebSites\McsWeb2" $target 
    $sourceFiles = Get-ChildItem $sourcePath 

    foreach ($file in $sourceFiles) { 
     Copy-Item $file -Destination $destPath 
    } 
} 

错误:

Remove-Item : Cannot find path 'C:\Program Files (x86)\Jenkins\jobs\MCSTest\workspace\App_Code' because it does not exist. At C:\Users\SVC-VI~1\AppData\Local\Temp\jenkins5893875881898738781.ps1:9 >char:1 9 + Remove-Item <<<< $file -Recurse -Force + CategoryInfo : ObjectNotFound: (C:\Program >File...kspace\App_Co de:String) [Remove-Item], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.Remov eItemCommand

Copy-Item : Cannot find path 'C:\Program Files (x86)\Jenkins\jobs\MCSTest\works pace\App_Code' because it does not exist. At C:\Users\SVC-VI~1\AppData\Local\Temp\jenkins5893875881898738781.ps1:16 >char: 18 + Copy-Item <<<< $file -Destination $destPath + CategoryInfo : ObjectNotFound: (C:\Program >File...kspace\App_Co de:String) [Copy-Item], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyI temCommand

纯碱,

既不的建议工作。它仍然会删除目标目录中的所有内容,并将源目录文件夹添加到没有文件的目标目录。我有点迷失在这里。

+0

任何新错误信息? – sodawillow

回答

0
  1. 你缺少你的路径反斜杠,使用Join-Path建立你的字符串,以避免(包括在files.txt可能导致反斜杠)

  2. 你反覆$dest$source但这些都是字符串,而不是文件集合,您应该能够使用Get-ChildItem $destGet-ChildItem $source进行检索,例如

此外,为便于阅读,您不应该使用脚本(rdcp)别名

PS:我相信你的脚本产生错误,你应该将它们包含在你的问题(you can edit it

关于编辑点评:

试试这个(未经测试,除去-WhatIf的处理):

$targetList = Get-Content "D:\files.txt" 

foreach ($target in $targetList) { 

    $destPath = Join-Path "D:\destination" $target 
    Remove-Item $destPath -Recurse -Force -WhatIf 

    $sourcePath = Join-Path "D:\source" $target 
    Copy-Item $sourcePath -Destination $destPath -Recurse -WhatIf 

} 

EDIT2:我修正并简化了上面的代码,我的逻辑稍微偏离了一点。

这甚至比这更简单,更好,到组中删除语句和运行它们的副本语句之前,如:

$targetList = Get-Content "D:\files.txt" 

#remove all 
$targetList | ForEach-Object { 
    Remove-Item (Join-Path "D:\destination" $_) -Recurse -Force -WhatIf 
} 

#copy all 
$targetList | ForEach-Object { 
    Copy-Item (Join-Path "D:\source" $_) (Join-Path "D:\destination" $_) -Recurse -Force -WhatIf 
} 

两个片段已经与样本文件夹结构测试。

为了完整起见,我在第一次尝试时得到的错误是由于将$file对象传递给处理cmdlet而不是完整路径($file.FullName)。

+0

前导反斜杠包含在files.txt中。发生的第一件事是它删除\ Server2 \ Folder B下的所有内容,然后在无法找到要删除的路径的地方开始抛出错误。 删除-项目:无法删除项\\ LTR1MCS04 \ MCSWebTest:因为它正被另一个进程使用 删除项目开展过程不能ACC ESS文件“\\ Server2上\文件夹B”:找不到路径“\ \ Server2 \ Folder B \ bin \ filname.dll',因为它不存在 。 我不太确定我要添加Get-ChildItem命令的位置。 – Kerry

+0

@Kerry我编辑了我的答案并添加了一个示例解决方案。如果您遇到错误,请将其包含在代码或报价格式的问题中(为了便于阅读)。 – sodawillow

+0

我非常感谢您的帮助,我已使用收到的代码和错误编辑了我的问题。 – Kerry