检查 CSV 是否为空

Check if the CSV is empty

我有一个只有一列的 CSV 文件。

**Fruits**
Apple
Orange

如何检查此 csv 是否为空,我的意思是 csv 文件中是否没有列出苹果或橙子。

我正在使用以下代码导入 csv,但我不确定如何在执行进一步命令之前检查导入后 csv 是否为空。

$path     = Split-Path -parent $MyInvocation.MyCommand.Definition
$csvPathforFruits = $path + "\Fruits.csv"
$importFruit = Import-Csv $csvPathforFruits

怎么样:

$importFruit = @(Import-Csv $csvPathforFruits)    
$importFruit.Length -gt 0

如果 CSV 中有任何 non-header 行,这将 return True

$importFruit = Import-Csv $csvPathforFruits | Measure-Object
$count = $importFruit.Count

$count 给出了 non-header 行的数量。在这种情况下;

**Fruits**
Apple
Orange

它应该给2。如果是0,它就是空的。