Reputation: 11
I'm looping through some files and want to prepend the filename to an existing file. I thought I'd got stuff going ok, but by the second time around the loop, I lose the existing end of the file, and get System.Object[] instead of the former contents.
A simple example here shows roughly what I'm doing to prepend the information, and replicates the outcome (which I don't want!).
$a = "1","2","3"
$b = "test"
$c = -join $b,$a
# At this point, $c is as I expect; it has added $b to the start of the list.
$a = $c
$c = -join $b,$a
# This time, when I try to add $b to the front of $a, it correctly adds the new line, and the second line is also correct, but then it dumps the object properties of the remaining lines (or something?)
The output is shown below
test
test
Length : 3
LongLength : 3
Rank : 1
SyncRoot : {1, 2, 3}
IsReadOnly : False
IsFixedSize : True
IsSynchronized : False
Count : 3
If you're writing this to a file using set-content, then it writes the object properties as System.Object[], which isn't what I want!
Thanks
Update: As per the comment on this original post, the solution I'm looking for is to use $a = (,$b)+$a. This has the effect of prepending the string in $b to the start of the array of strings stored in $a.
Upvotes: 1
Views: 1029
Reputation: 1598
I think, you need some clarification here:
$a = "1","2","3"
$b = "test"
$c = -join $b,$a
does not create an array $c
containing
'test', '1', '2', '3'
instead $c
looks like this:
'test', ('1', '2', '3')
What I think you wanted to do is:
$c = $b , $a
which results in
'test', '1', '2', '3'
Upvotes: 2