0

I have recently changed my script to import data from a csv file to a tab separated file. The issue that I am having is when there is a blank value in the tab separated file in say $var1 it does not treat $var as either NULL or "", I was thinking there is a tab stored in the variable, so I striped the variabled from containing tabs, the same issue still occurring. The tab separated file contains 6 columns and 5 rows and the headers are added when the file is imported using the below command.

My question is what will be read from a tab separated file if one of the columns is blank?

        The command used to import the files contents.
        $List = Import-Csv C:\Users\jsmith\Documents\file.out -Delimiter "`t" -Header "ID","Date","First Name","Last Name","Gender"
user3175140
  • 511
  • 3
  • 6
  • 15
  • I was unable to reproduce your bug. When I tried it with sample data, the missing value was an empty string: 1 \t 6/10/2014 \t John \t Doe \t M \t X 2 \t 6/01/2014 \t Frank \t O'Connor \t M \t X 3 \t 4/10/2012 \t Fred \t \t M \t X $List[2].'Last Name'.GetType() returned System.String, and $List[2].'Last Name'.Length returned 0 – JamesQMurphy Jun 19 '14 at 04:51
  • I am receiving different results for different variables. When $var.length for one variable it returns as 0. When $var1.length for another I dont receive anything back. When I $var.gettype() it displays as a string however when I $var1.gettype() I receive the error 'You cannot call a method on a null-valued expression.' Both the variables are empty after importing from the tab separated file. – user3175140 Jun 20 '14 at 00:33
  • I have broken it down as shown below, I have used different header names from my original post; $List = Import-Csv C:\Users\jsmith\Documents\file.out -Delimiter "`t" -Header "ID","Date","First Name","Mid. Name","postOfficeBox" ForEach ($record in $List) { $middleName_file_value = $record."Mid. Name" $middleName_file_value $POBox_file_value = $record.postOfficeBox $POBox_file_value } – user3175140 Jun 20 '14 at 00:38

1 Answers1

1

Try evaluating for [System.DBNull]::Value.

I typically assign it to a variable to make it easy to evaluate later:

$DBnull = [System.DBNull]::Value
if ($value -eq $DBNull) {
    # Do stuff
    }
Tim Ferrill
  • 1,648
  • 1
  • 12
  • 15
  • Evaluating for [System.DBNull]::Value is answer, I was originally evaluating for $NULL and didn't resolve the issue however [System.DBNull]::Value does :) What is the difference between the two. – user3175140 Jun 20 '14 at 00:50
  • This may help explain it a little bit: http://stackoverflow.com/questions/4958379/what-is-the-difference-between-null-and-system-dbnull-value – Tim Ferrill Jun 20 '14 at 01:08
  • Thank you for the information, this has answered my question. – user3175140 Jun 20 '14 at 01:42