2017-05-27 264 views
1

我只有一列的CSV文件。检查CSV是否为空

**Fruits** 
Apple 
Orange 

我如何检查,如果这个CSV是空的,我的意思是如果没有苹果或橘子在CSV文件中列出。

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

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

回答

4

如何:

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

这将返回True如果有在CSV任何非标题行。

+0

它返回False,如果我只是添加苹果 –

+0

更新答案总是强制结果到一个数组 – lesscode

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

$ count给出了非标题行的数量。在这种情况下;

**Fruits** 
Apple 
Orange 

它应该给出2.如果它是0,它将是空的。