![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||