Announcement

Collapse
No announcement yet.

Import-csv file issue

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Import-csv file issue

    Hi
    i have a problom to find out the solution to this problem.

    i have a csv file containing a number of rows
    ex:

    User, Number1,Number2,Code
    user1, 123456789,123456,abcd
    user2, 2345678901,234567,bcde
    user3 3456789012,345678,cdef

    And what I need to do is to pick out the number1 value on each line
    convert it with this

    [Convert]::ToString($a, 16)
    $c =[Convert]::ToString($a, 16)
    $d = $c.Substring(6)
    $e = [Convert]::ToInt32($d, 16)

    (the varaibels are just what i used for testing)

    And then put it back into the file or create a new file with a new number1 value

    ex;
    User, Number1,Number2,Code
    user1, converted value,123456,abcd
    user2, converted value,234567,bcde
    user3 converted value,345678,cdef

    anyone here that can point me in the correct direction?

  • #2
    Assuming $a represents the Number1 column and $e represents what you want to write to it, this script should do the job:
    Code:
    $exportdir = 'c:\folder\outfile.csv'
    $importdir = 'c:\folder\infile.csv'
    
    $csvfile = Import-Csv $importdir
    
    Foreach($a in $csvfile){
        $c =[Convert]::ToString($a.Number1, 16)
        $d = $c.Substring(6)
        $e = [Convert]::ToInt32($d, 16)
        $a.Number1 = $e
    }
    
    $csvfile | Export-Csv $exportdir
    What I did was import the CSV, looped through it and performed the conversions on the specified field, wrote back the conversion to the field, then exported it to a new CSV file.
    Regards,
    Jeremy

    Network Consultant/Engineer
    Baltimore - Washington area and beyond
    www.gma-cpa.com

    Comment

    Working...
    X