Weird question

This topic contains 6 replies, has 5 voices, and was last updated by  Simon B 3 weeks, 3 days ago.

  • Author
    Posts
  • #82723

    Tony Antony
    Participant

    I have the code below where it grabs info from a CSV file. The problem is that, not everyone have a title, or a desk phone, or a cell phone. I'm able to write if title is NULL, don't read at line. This is working.

    I was wondering if there's a way in PowerShell to tell it if any of these (Name, Title, Street, etc) is NULL, don't read that line?

    I hope I'm being clear on what I'm asking.

      (Get-Content $html_files_location\$txt_file_names) `
         -creplace ("NAME",$employees.Name) `
          -creplace ("TITLE",$employees.Title) `
           -creplace ("STREET", $employees.Street) `
            -creplace ("ADDRESS_INFO", $employees.Address_info) `
             -creplace ("PHONE", $employees.Phone) `
              -creplace ("CELL", $employees.Cell) `
               -creplace ("WEBSITE", $web) `
                -creplace ("ADURL", $web.Trim("http://")) `
                 -creplace ("LOGO", $logo) ` |
        
        Set-Content $html_files_location\$txt_file_names -Force
  • #82727

    Why aren't you using Import-Csv cmdlet?

    • #82730

      Tony Antony
      Participant

      I am using that command. The above code is just a portion of what I have. I didn't copy the whole thing.

  • #82742

    Don Jones
    Keymaster

    If you're using Import-CSV, then each row is an object. Assuming you're enumerating the rows and using a variable like $row (since you didn't post that part, I can only provide a basic example) I'd probably do something like...

    if ($row.website -and $row.name -and $row.title) {
    # all three fields contain something
    }

  • #82744

    Matt Bloomfield
    Participant

    Just be careful whether the value is NULL or just an empty string, they're not the same thing. You should pipe your row to Get-Member to check.

    As for skipping rows that contain an empty cell, this should work:

    $csv = Import-Csv test.csv
    
    foreach ($row in $csv) {
    
        if (-not($row.PSObject.Properties.Value -contains '')) {
    
            Write-Output $row
      
        } #end if
    
    } #end foreach
    
  • #82748

    Tony Antony
    Participant

    Thank you

  • #82753

    Simon B
    Participant

    This should also work

    $stripedcsv = Import-Csv C:\names\names.csv | ? { ($_.PSObject.Properties | % {$_.Value}) -notcontains "}

You must be logged in to reply to this topic.