[英]Figuring out an array using powershell
我正在尝试创建一个数组,该数组将从目标路径中删除文件,然后从源路径复制到目标路径。 我已经在构建服务器上创建了一个.txt文档,其中包含文件列表及其相对路径。 当我运行以下代码块时,它将删除文件夹B中的所有内容,并将文件夹A(不包含任何内容)复制到文件夹B。
这就是我在跑步
$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:找不到路径“ C:\\ Program Files(x86)\\ Jenkins \\ jobs \\ MCSTest \\ workspace \\ App_Code”,因为该路径不存在。 在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)[删除项],ItemNotFoundException + FullyQualifiedErrorId:PathNotFound,Microsoft.PowerShell.Commands.Remov eItemCommand
复制项:找不到路径'C:\\ Program Files(x86)\\ Jenkins \\ jobs \\ MCSTest \\ works speed \\ App_Code',因为它不存在。 在C:\\ Users \\ SVC-VI〜1 \\ AppData \\ Local \\ Temp \\ jenkins5893875881898738781.ps1:16> char:18 +复制项<<<< $ file -Destination $ destPath + CategoryInfo:ObjectNotFound:(C:\\程序>文件... kspace \\ App_Co de:String)[Copy-Item],ItemNotFoundException + FullyQualifiedErrorId:PathNotFound,Microsoft.PowerShell.Commands.CopyI temCommand
苏打,
两项建议均无效。 它仍然删除目标目录中的所有内容,并将源目录文件夹添加到不包含文件的目标目录中。 我在这里迷路了。
您在路径中缺少反斜杠,请使用Join-Path
生成字符串来避免这种情况( 或者也许在files.txt
中包含前导反斜杠)
您可以在$dest
和$source
上进行迭代,但是这些是字符串,而不是文件集合,例如,应该可以使用Get-ChildItem $dest
和Get-ChildItem $source
进行检索。
另外,出于可读性考虑,您不应在脚本( rd
和cp
)中使用别名
PS:我相信您的脚本会产生错误,您应该在问题中包含它们( 可以对其进行编辑 )
编辑评论:
尝试以下操作(未经测试,删除-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:我已更正并简化了上面的代码,我的逻辑略有偏离。
将remove语句分组并在copy语句之前运行它们甚至可能更简单更好,例如:
$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
)。
问题是您尝试循环太多次。 外循环一次处理一个文件。 应该没有内部循环。 此外,在发出删除操作之前,您无需验证文件是否存在。
$files = Get-Content "C:\test.txt"
$source_base="C:\TEMP\source\"
$dest_base="C:\TEMP\dest\"
foreach ($filepath in $files)
{
$source = $source_base + $filepath
$dest = $dest_base + $filepath
if(Test-Path $dest) {rd $dest -recurse -force}
cp $source -destination $dest
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.