-1

I have a csv that I want to check the count of rows and then loop through the contents. I'm using the code at the bottom to get the count which works but I'm not sure how I can loop through the csv and get the values in each column.

I've read that I can do it using the select-object cmdlet if I specify the column names however this code will work on a number of csv's all with different column names. How can I make this work?

$csv = Import-Csv -Path $requestFile  | measure

if(($csv).count-1 -gt 1){
    //do something
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Richard Banks
  • 2,946
  • 5
  • 34
  • 71
  • If I call `Import-Csv` without piping the result to anything (e.g. `Import-Csv .\test.csv`) then it shows me the property of each row, one property per line. Are you looking for a different output format? – ama1111 Sep 30 '15 at 16:25
  • @ama1111 yes but then you cant get the count – Richard Banks Oct 01 '15 at 07:45
  • Removing the pipe to `Measure` will allow you to get both the count and the data. I posted an answer with more details. – ama1111 Oct 01 '15 at 16:01

2 Answers2

0

Import-Csv creates a list of objects. This list already has a Count property, so you don't need to measure it yourself:

$csv = Import-Csv -Path $requestFile

if ($csv.Count -gt 2) {
    # do something
}

Not sure why you'd want to restrict the "do someting" to CSVs with more than 2 rows, though.

If you also want to loop over the columns of each row you can do that with a nested loop as described in this answer:

$csv = Import-Csv -Path $requestFile

if ($csv.Count -gt 2) {
    $csv | ForEach-Object {
        foreach ($property in $_.PSObject.Properties) {
            doSomething $property.Name, $property.Value
        } 
    }
}

For further help you'd need to explain what you actually want to do with the columns.

Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • The way this answer is currently written makes it sound like a comment. Is this a comment, or an answer? If this is an answer, it should be written in a more concrete way. – JAL Oct 04 '15 at 23:11
0

You don't need to pipe to Measure to get the row count. In fact, the variable you've stored in $csv is not the csv data but the output from Measure, so you should remove the pipe to Measure.

Here's an example:

PS C:\temp> $csv = Import-Csv .\test.csv
PS C:\temp> # Here you can perform your check on the size of the csv
PS C:\temp> $csv.Count
4
PS C:\temp> # ... and you can get all the data like this:
PS C:\temp> $csv


Year        : 1997
Make        : Ford
Model       : E350
Description : ac, abs, moon
Price       : 3000.00

Year        : 1999
Make        : Chevy
Model       : Venture "Extended Edition"
Description :
Price       : 4900.00

Year        : 1999
Make        : Chevy
Model       : Venture "Extended Edition, Very Large"
Description :
Price       : 5000.00

Year        : 1996
Make        : Jeep
Model       : Grand Cherokee
Description : MUST SELL!
              air, moon roof, loaded
Price       : 4799.00

My csv looks like this:

Year,Make,Model,Description,Price
1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""","",4900.00
1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00
1996,Jeep,Grand Cherokee,"MUST SELL!
air, moon roof, loaded",4799.00
ama1111
  • 569
  • 3
  • 10