Add char to every line of text

This topic contains 10 replies, has 3 voices, and was last updated by Profile photo of Stefan Cvetkovic Stefan Cvetkovic 1 month, 1 week ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #50813
    Profile photo of Stefan Cvetkovic
    Stefan Cvetkovic
    Participant

    Hello,

    I want to add for each line of the text some chars in front of it.
    Here is my code:

    $InputFile = $content2+$content3+$content4
    ForEach($Obj in $InputFile)
    {
    $everyLineChar = "* "
    $combine = $end+$Obj
    Write-Host $InputFile
    }

    I am a newbie when it comes to PowerShell. I am a self-study guy so.. 🙂

    #50832
    Profile photo of thom schumacher
    thom schumacher
    Participant

    $a = get-content c:\temp\somefile.txt
    foreach($b in $a){$char = 'ok '; $c= "$char$b" ; $c}

    $c has the contents of $char and $b that contains each line in the file you got for $a

    #50835
    Profile photo of Stefan Cvetkovic
    Stefan Cvetkovic
    Participant

    Thanks for your fast reply!

    However, I've tried to incorporate this logic into my code but, it has been written just in the first line, but the others stayed intact.
    Here is my full code, to get a bigger picture – I am pulling out some data from some cells, and I want to add into another document and to add asterix for each row.

    $objWord = New-Object -ComObject Word.Application
    $objWord.Visible = $true
    $filename = 'D:\test.docx'
    $objDocument = $objWord.Documents.Open($filename)

    $LETable = $objDocument.Tables.Item(1)
    $LETableCols = $LETable.Columns.Count
    $LETableRows = $LETable.Rows.Count

    #Write-output "Starting to write... "

    #Write-output "Starting to write... "

    #$asterix = "* "
    #Write-Host $LETable.Cell(6,2).Range.Text|foreach {$_ + "*"}
    $content2 = $LETable.Cell(6,2).Range.Text -replace '\r\a'," -creplace '(?m)^\s*\r?\n',"
    $hl = "`n"
    $content3 = $LETable.Cell(7,1).Range.Text -replace '\r\a'," -creplace '(?m)^\s*\r?\n',"
    $content4 = $LETable.Cell(7,2).Range.Text -replace '\r\a'," -creplace '(?m)^\s*\r?\n',"
    $doc2 = $objWord.Documents.Add()
    $InputFile = $content2+$hl+$content3+$content4
    foreach($b in $InputFile){$char = '* '; $c= "$char$b" ; $c}
    $objWord.Selection.typetext("$c")

    #foreach ($Line in $objWord.Selection.typetext("$content2$hl$content3$content4")) {
    #$Line = $line.Insert(0,'* ')
    #$Line += '"'}
    #$ende = $content2+$hl+$content3+$content4
    #$ende|foreach {$_ + "*"}
    #$objWord.Selection.typetext("$ende")
    #Write-Host $ende

    #$objWord.Selection.typetext("$content3")
    #$objWord.Selection.typetext("$content4")
    #$objDocument.Close()
    #$objWord.Quit()
    # Stop Winword Process
    #$rc = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objWord)

    #50843
    Profile photo of thom schumacher
    thom schumacher
    Participant

    Not sure if this is what you are looking for but I think It might be close:

    $objWord = New-Object -ComObject Word.Application
    $objWord.Visible = $true
    $filename = "$env:USERPROFILE\Documents\test.docx"
    $objDocument = $objWord.Documents.Open($filename)
    
    $LETable = $objDocument.Tables.Item(1)
    $LETableCols = $LETable.Columns.Count
    $LETableRows = $LETable.Rows.Count
    
    #Write-output "Starting to write... "
    
    #Write-output "Starting to write... "
    
    #$asterix = "* "
    #Write-Host $LETable.Cell(6,2).Range.Text|foreach {$_ + "*"}
    $content2 = $LETable.Cell(6,2).Range.Text -replace '\r\a'," -creplace '(?m)^\s*\r?\n',"
    $hl = "`n"
    $content3 = $LETable.Cell(7,1).Range.Text -replace '\r\a'," -creplace '(?m)^\s*\r?\n',"
    $content4 = $LETable.Cell(7,2).Range.Text -replace '\r\a'," -creplace '(?m)^\s*\r?\n',"
    $doc2 = $objWord.Documents.Add()
    $InputFile = ($content2,$hl,$content3,$content4)
    foreach($b in $InputFile){$char = '* '; $c= "$char$b" ; $c}
    $objWord.Selection.typetext("$c")
    

    Changed this line: $InputFile = ($content2,$hl,$content3,$content4)
    To be an array so that the foreach had more than One item to iterate through.
    The way you had it concatenated together it wasn't an Array which is what Foreach operates on best.

    Thom

    #50918
    Profile photo of Stefan Cvetkovic
    Stefan Cvetkovic
    Participant

    Thom, thank you very much. It is close, but it puts only for one string an asterix – "*".
    I need to dive into each string and to ask where is a new line and for each new line to put "*"

    Thanks.

    #51055
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Hi Stefan,
    It sounds like you are referring to what look like new line withing a cell of a table in Word. These are not new lines, they are Vertical Tabs. You can detect these with RegEx using \v

    For Example:

    $objWord = New-Object -ComObject Word.Application
    $objWord.Visible = $true
    $filename = "C:\Users\user\Desktop\New Microsoft Word Document.docx"
    $objDocument = $objWord.Documents.Open($filename)
    
    $LETable = $objDocument.Tables.Item(1)
    $LETableCols = $LETable.Columns.Count
    $LETableRows = $LETable.Rows.Count
    
    #Write-output "Starting to write... "
    
    #Write-output "Starting to write... "
    
    #$asterix = "* "
    #Write-Host $LETable.Cell(6,2).Range.Text|foreach {$_ + "*"}
    $content2 = "*$($LETable.Cell(1,6).Range.Text -replace '\v', "*")*"
    $content2
    

    Results in

    *Asdf*fdas*1234*4312*
    

    for Cell 1,6 in a table that contains the following data

    Asdf
    fdas
    1234
    4312
    
    • This reply was modified 1 month, 1 week ago by Profile photo of Curtis Smith Curtis Smith.
    #51106
    Profile photo of Stefan Cvetkovic
    Stefan Cvetkovic
    Participant

    Dear Curtis,

    Unfortunately, I couldn't perform successful result.
    Maybe my data in word are not god formatted, although I've checked several times..
    Please, could you upload your example, or maybe you want my file?
    I've managed to do it through C#, but I want to show my friends that power-shell can do it also 🙂

    Thanks!

    edit: here is my output:
    image

    #51174
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Would be best for you to provide a sample of the data you are working with, that way we can see the challenge. If you have this working with C#, provide a screenshot of the output your are expecting and want to duplicate with PowerShell along with the data sample.

    #51182
    Profile photo of Stefan Cvetkovic
    Stefan Cvetkovic
    Participant

    Hi and thank you Curtis.

    Here is my folder with two files.
    First docx file is the file that contains a table, and I highlighted what I want to be taken from it.
    And .txt file with "*" in each row.
    test folder

    Thanks again!

    #51270
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Here's how to do it

    $objWord = New-Object -ComObject Word.Application
    $objWord.Visible = $true
    $filename = "D:\Temp\file - This I get as an input object.doc"
    $objDocument = $objWord.Documents.Open($filename)
    
    $LETable = $objDocument.Tables.Item(1)
    $LETableCols = $LETable.Columns.Count
    $LETableRows = $LETable.Rows.Count
    
    #Write-output "Starting to write... "
    
    #Write-output "Starting to write... "
    
    #$asterix = "* "
    
    # -We will just collect all of our values into a single $output variable to use when we type to the new document
    # -First we Replace all of the Carriage Return characters with a Carriage Return, an *, and a space.  This puts
    #  an * at the beginning of each line.
    # -We then replace the bel character with nothing because we don't want it.
    # -The result is a string that has no starting *, but has an * at every Carriage Return after that as well as an
    #  * and a space at the very end.
    $output = $LETable.Cell(6,2).Range.Text -replace "`r", "`r* " -replace "`a"
    $output += $LETable.Cell(7,1).Range.Text -replace "`r", "`r* " -replace "`a"
    $output += $LETable.Cell(7,2).Range.Text -replace "`r", "`r* " -replace "`a"
    
    $doc2 = $objWord.Documents.Add()
    
    # -Then when we type our text, we add an * and space to the very beginning, and trim the * and space off the very
    #  end.
    $objWord.Selection.typetext("* $($output.TrimEnd("* "))")
    
    • This reply was modified 1 month, 1 week ago by Profile photo of Curtis Smith Curtis Smith.
    • This reply was modified 1 month, 1 week ago by Profile photo of Curtis Smith Curtis Smith.
    #51422
    Profile photo of Stefan Cvetkovic
    Stefan Cvetkovic
    Participant

    I was so close! Thank you Curtis! I got still to learn 🙂

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.