Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Adding and deleting entries i a CSV file

Reply
 
Old 11-21-2007   #1 (permalink)
MichielV


 
 

Adding and deleting entries i a CSV file

Hi,

I want to add and delete some entries in a CSV file:

$file = "c:\test.csv"
$h = import-csv $file # import a csv file
$len = $h.count
$x = $h[0] # $x is a copy of the first element
$h = $h + $x # $h[$len] is added and the contents is a
copy of the first element
$h[$len].<property1> = "bla bla" # change a property of the new element

The problem is that the last line will also change $h[0].<property1>. It
seems that some kind of pointer
was created when I did: $x = $h[0] and $h = $h + $x

My questions:
1) How can I add a new entry to $h?
2) How can I delete an entry?


regards

Michiel

My System SpecsSystem Spec
Old 11-21-2007   #2 (permalink)
Shay Levi


 
 

Re: Adding and deleting entries i a CSV file

I tried Remove-Item and Remove-ItemProperty, none designed for this kind
of task.

Here's a workaround to remove items:


# create test csv file with process names and id's
get-process | select id,name | export-csv .\test.csv

# import test csv file
$csv = import-csv .\test.csv

# add another column to tag each process
# adds 0 to all items Tag property
$csv | Add-Member noteproperty Tag 0

# view changes
#$csv | ft -auto

# tag each non desired item with 1
$csv[0].tag=1

# export to new csv file
$csv | where {$_.tag -ne 1} | select id,name | export-csv .\test1.csv



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com


Quote:

> Hi,
>
> I want to add and delete some entries in a CSV file:
>
> $file = "c:\test.csv"
> $h = import-csv $file # import a csv file
> $len = $h.count
> $x = $h[0] # $x is a copy of the first element
> $h = $h + $x # $h[$len] is added and the contents
> is a
> copy of the first element
> $h[$len].<property1> = "bla bla" # change a property of the new
> element
> The problem is that the last line will also change $h[0].<property1>.
> It
> seems that some kind of pointer
> was created when I did: $x = $h[0] and $h = $h + $x
> My questions:
> 1) How can I add a new entry to $h?
> 2) How can I delete an entry?
> regards
>
> Michiel
>

My System SpecsSystem Spec
Old 11-21-2007   #3 (permalink)
Kiron


 
 

Re: Adding and deleting entries i a CSV file

Here are two ways of adding an entry to your CSV data:

# adding a copied entry
$h = @(import-csv c:\test.csv)
$len = $h.count
$properties = $h |
get-member -type NoteProperty
$x = new-object psObject
$properties | foreach {
add-member NoteProperty $_.name $h[0].$($_.Name).clone() -in $x
}
$h += $x
$h[$len].property1 = 'blah blah'
$h


# adding a brand new entry
$h = @(import-csv c:\test.csv)
$x = new-object psObject |
select property1, property2, property3, property4
$x.property1 = 'blah 1'
$x.property2 = 'blah 2'
$x.property3 = 'blah 3'
$x.property4 = 'blah 4'
$h += $x
$h

Since $h is an [object[]] you can slice it to remove entries. To delete the third entry:

$h = $h[0,1,3,4]

# or use range operator expressions
$h = $h[0..1 + 3..($h.length - 1)]

To remove the second:

$h = $h[0,2,3,4]
$h = $h[0..0 + 2..($h.length - 1)]

Of course, to save the data, export it:
$h | export-csv c:\exported.csv -noType

--
Kiron
My System SpecsSystem Spec
Old 11-21-2007   #4 (permalink)
Kiron


 
 

Re: Adding and deleting entries i a CSV file

You can also filter entries and keep those that meet certain criteria discarding those that do not:

$h = $h | where {$_.property1 -notMatch 'blah' -and
$_.property3 -lt 3}

--
Kiron
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
deleting old calendar entries Live Mail
Solved Problems adding new MBR entries General Discussion
Vista - Mail Client - deleting Recipient pull down entries Vista General
Adding Services Entries Vista General
Adding static ARP entries fail in Vista Vista networking & sharing


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46