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 - Edit Text File and Save

Reply
 
Old 06-20-2007   #1 (permalink)
Greg


 
 

Edit Text File and Save

I have a situation where I need to add " to the beginning of the text in a
..csv file that is output by Sql Server BCP. I've tried the following but for
some reason the file size goes down instead of up and the file won't open
after it's written. Anyone have an idea?

$FileContents = Get-Content -Path C:\Production.csv
$Quotes = '"'
$FileContents = $Quotes + $FileContents
$TodayDate = get-date -uformat "%m-%d-%Y"
New-Item -Path C:\Production$TodayDate.csv -ItemType file
Set-Content -path C:\Production$TodayDate.csv -value $FileContents

I also need to be able to trim a trailing " from the end of the file but I
can't figure out how to do that either.


My System SpecsSystem Spec
Old 06-20-2007   #2 (permalink)
Hal Rottenberg


 
 

Re: Edit Text File and Save

On Jun 20, 1:51 pm, Greg <G...@discussions.microsoft.com> wrote:
> I have a situation where I need to add " to the beginning of the text in a
> .csv file that is output by Sql Server BCP. I've tried the following but for


> I also need to be able to trim a trailing " from the end of the file but I
> can't figure out how to do that either.


Do you mean to prepend a quote at the beginning and end of every
line? Or at the beginning and end of the file?

My System SpecsSystem Spec
Old 06-20-2007   #3 (permalink)
Greg


 
 

Re: Edit Text File and Save

All I need is to add a quote at the beginning of the first line and delete a
quote from the end of the last line.

"Hal Rottenberg" wrote:

> On Jun 20, 1:51 pm, Greg <G...@discussions.microsoft.com> wrote:
> > I have a situation where I need to add " to the beginning of the text in a
> > .csv file that is output by Sql Server BCP. I've tried the following but for

>
> > I also need to be able to trim a trailing " from the end of the file but I
> > can't figure out how to do that either.

>
> Do you mean to prepend a quote at the beginning and end of every
> line? Or at the beginning and end of the file?
>
>

My System SpecsSystem Spec
Old 06-20-2007   #4 (permalink)
Jeff


 
 

Re: Edit Text File and Save

On Jun 21, 1:29 am, Greg <G...@discussions.microsoft.com> wrote:
> All I need is to add a quote at the beginning of the first line and delete a
> quote from the end of the last line.
>
> "Hal Rottenberg" wrote:
> > On Jun 20, 1:51 pm, Greg <G...@discussions.microsoft.com> wrote:
> > > I have a situation where I need to add " to the beginning of the text in a
> > > .csv file that is output by Sql Server BCP. I've tried the following but for

>
> > > I also need to be able to trim a trailing " from the end of the file but I
> > > can't figure out how to do that either.

>
> > Do you mean to prepend a quote at the beginning and end of every
> > line? Or at the beginning and end of the file?


The file size was going down because you were losing your newline
characters at the end of each line. This happens when you treat the
contents of the file as one string ($FileContents = $Quotes +
$FileContents), rather than a array of strings, which is what Get-
Content returns. It actually returns a array of objects, but since
this is a text file, we can assume they are strings.

There might be a better way to do it, but this seems to do the trick:

$FileContents = Get-Content -Path C:\Production.csv

$Quotes = '"'
$TodayDate = get-date -uformat "%m-%d-%Y"

$FileContents[ 0 ] = $Quotes + $FileContents[ 0 ]
# a negative index starts from the end of the file, so -1 gets the
# last line of the file, -2 the second to last, etc.
$FileContents[ -1 ] = $FileContents[ -1 ] -replace "$Quotes$"

$FileContents | Set-Content -Path C:\Production$TodayDate.csv

If there are multiple empty lines at the end of your original CSV
file, you may have to adjust the negative index.

Jeff

My System SpecsSystem Spec
Old 06-21-2007   #5 (permalink)
Greg


 
 

Re: Edit Text File and Save

Jeff, thanks a bunch. That was it. Makes perfect sense now and it works!

Greg

"Jeff" wrote:

> On Jun 21, 1:29 am, Greg <G...@discussions.microsoft.com> wrote:
> > All I need is to add a quote at the beginning of the first line and delete a
> > quote from the end of the last line.
> >
> > "Hal Rottenberg" wrote:
> > > On Jun 20, 1:51 pm, Greg <G...@discussions.microsoft.com> wrote:
> > > > I have a situation where I need to add " to the beginning of the text in a
> > > > .csv file that is output by Sql Server BCP. I've tried the following but for

> >
> > > > I also need to be able to trim a trailing " from the end of the file but I
> > > > can't figure out how to do that either.

> >
> > > Do you mean to prepend a quote at the beginning and end of every
> > > line? Or at the beginning and end of the file?

>
> The file size was going down because you were losing your newline
> characters at the end of each line. This happens when you treat the
> contents of the file as one string ($FileContents = $Quotes +
> $FileContents), rather than a array of strings, which is what Get-
> Content returns. It actually returns a array of objects, but since
> this is a text file, we can assume they are strings.
>
> There might be a better way to do it, but this seems to do the trick:
>
> $FileContents = Get-Content -Path C:\Production.csv
>
> $Quotes = '"'
> $TodayDate = get-date -uformat "%m-%d-%Y"
>
> $FileContents[ 0 ] = $Quotes + $FileContents[ 0 ]
> # a negative index starts from the end of the file, so -1 gets the
> # last line of the file, -2 the second to last, etc.
> $FileContents[ -1 ] = $FileContents[ -1 ] -replace "$Quotes$"
>
> $FileContents | Set-Content -Path C:\Production$TodayDate.csv
>
> If there are multiple empty lines at the end of your original CSV
> file, you may have to adjust the negative index.
>
> Jeff
>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How to edit a text file (remove carriage returns) VB Script
view or modify text with edit PowerShell
paint cannot edit text Vista General
Gmail IMAP: Can't edit messages after save Live Mail
CANT SAVE A PICTURE AFTER I EDIT IT. Vista music pictures video


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