本文介绍了空文件夹上的 Powershell 比较对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的 DLL 复制脚本来帮助我的开发团队设置他们的本地环境.我检查了 build devdrop 文件夹并获得了一个 DLL 文件列表.然后我查看本地文件夹并复制任何较新的 DLL.

I am writing a simple DLL copying script to help my dev team in setting up their local environments. I check the build devdrop folder and get a list of DLL files. I then look at the local folder and copy any newer DLLs over.

我的问题是当本地文件夹为空时(即脚本第一次运行),它会崩溃.

My problem is when the local folder is empty (i.e. the first time the script runs) it all falls apart.

$newFiles = Get-ChildItem -recurse -path "$drop\$latest\ship" -name
$oldFiles = Get-ChildItem -recurse -path $bin -name

$files = Compare-Object $newFiles $oldFiles -IncludeEqual -PassThru
$files | Where-Object { $_.SideIndicator -eq '<=' -or $_.SideIndicator -eq '=='} | % {
    Copy-Item -Path "$drop\$latest\ship\$_" -Destination $bin -Force
}

当 $bin 为空时,gci 调用将 $oldFiles 保留为空,给我一个很好的错误:

When $bin is empty the gci call leaves $oldFiles as null, giving me a nice error:

Compare-Object : 无法将参数绑定到参数DifferenceObject",因为它为空.

我已经四处搜索了,比较对象的所有操作方法似乎都没有解决这个问题.我可以检查文件夹是否为空,然后将所有文件复制过来,但我想知道是否有更好的方法.

I have searched around and all how-tos for Compare-Object don't seem to deal with this. I could check if the folder is empty and then copy all the files over, but I was wondering if there was a better way.

干杯.

推荐答案

$newFiles = @(Get-ChildItem -recurse -path "$drop\$latest\ship" -name)
$oldFiles = @(Get-ChildItem -recurse -path $bin -name)
$files = Compare-Object -ReferenceObject $newFiles -DifferenceObject $oldFiles -IncludeEqual -PassThru

这篇关于空文件夹上的 Powershell 比较对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 11:33
查看更多