![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||