MG2016
MG2016

Reputation: 319

Powershell - check if csv row has data

Trying to get this snippet I found to work for me. I need to check a folder and see if there are any files. If so, go through each one and check if at least one row of data exists (excluding the headers). Anyone help me out? Not sure how to read the files and then check the rows. Or any other ways to achieve this?

$csvData = Import-Csv "C:\Users\MG\Desktop\ScanFolder"                       
{
foreach($dataRow in $csvdata){
    $result = #dosomething} 
    if($result ){
        Write-Host "Data Exists"
    }
    else{
        Write-Host "Empty"
    }
}}

Upvotes: 1

Views: 4723

Answers (1)

Jeff Zeitlin
Jeff Zeitlin

Reputation: 10754

If you Import-CSV a CSV file with only the header record, it will succeed, but the result will be an array of length zero. Given your request, you should be able to do something like

$csvpath = "C:\CSVFiles"
ForEach ($file in (Get-ChildItem $csvpath)) {
    If ((Import-CSV $file).length -eq 0) {
        Write-Host "$file has no data"
    }
}

Upvotes: 3

Related Questions