我想创建一个数组,将从目标路径中删除文件,然后从源路径复制到目标路径。我已经在构建服务器上创建了一个.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
纯碱,
既不的建议工作。它仍然会删除目标目录中的所有内容,并将源目录文件夹添加到没有文件的目标目录。我有点迷失在这里。
任何新错误信息? – sodawillow