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 - Unable to Create New Line in Text File

Reply
 
Old 09-10-2009   #1 (permalink)
mory


 
 

Unable to Create New Line in Text File

TEMPLATE.scp contains
"blah

SomeText

blah"


I’m trying to create a text file and replace ‘SomeText’ with
"blah

OtherText1
OtherText2

blah"

What I get is:

"blah

OtherText1[]OtherText2

blah"


Here is the code:


$oldFile = "C:\TEST\TEMPLATE.scp"
$newFile = "C:\TEST\LIVE.scp"
$NewText = ""

Function UpdateNewFile($NewText)
{

(Get-Content $oldFile) |
ForEach-Object `
{
$_ -replace 'SomeText', $NewText

} | Set-Content $newFile
}


("1","2") | ForEach-Object {

$NewText = $NewText + "OtherText" + $_ + "`r"

}

UpdateNewFile($NewText)

My System SpecsSystem Spec
Old 09-10-2009   #2 (permalink)
Robert Robelo


 
 

Re: Unable to Create New Line in Text File

Windows' Newline is "`r`n", you could just replace the "`r" with "`r`n" in the statement that builds $NewText, but you'll end up with an extra blank line. I'd suggest to create a String[] and pass that to your function, then join the array with "`r`n" and use that String in the -replace command.

# compare...
[Int[]][Char[]][Environment]::NewLine
# ...with
[Int[]][Char[]]"`r`n"

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$oldFile = "C:\TEST\TEMPLATE.scp"
$newFile = "C:\TEST\LIVE.scp"

Function UpdateNewFile([String[]]$NewText) {
# join the array with "`r`n" through a call to String's Join static
# method, the output is a String. In v2 you'd use the -join operator
$NewText = [String]::Join("`r`n", $NewText)

# do the replace w/o the ForEach-Object Cmdlet and set the content
# to the new file
(Get-Content $oldFile) -replace 'SomeText', $NewText |
Set-Content $newFile
}

# set the new text as a String[] (String array)
[String[]]$NewText = 1,2 | ForEach-Object {"OtherText$_"}

# pass the arguments to your function _w/o_ parenthesis, that is a
# common mistake. In PowerShell, arguments are space-delimited
UpdateNewFile $NewText


--
Robert
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Unable to create a new text doc using right click Vista General
How to read the LAST NON-BLANK line from a text file? VB Script
Read a line from a text file, without loading the entire file inmemory PowerShell
How to remove blanks line from a text outpu file PowerShell
The next line in a text file PowerShell


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