question

SuzanaEree-2102 avatar image
0 Votes"
SuzanaEree-2102 asked ·

PowerShell: How to add a new blank line at every 30 words in several text files that contains 1000 words?

hello, is is possible to add a new blank line at every 30 words in several text files that contains 1000 words using PowerShell?

windows-server-powershell
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered ·

Hi,

Please check to see if this works.

 $files = "C:\temp\file1.txt","C:\temp\file2.txt"
 $results = "C:\temp\result1.txt","C:\temp\result2.txt"
 for($i=0;$i -lt $files.Count;$i++){
     $content = Get-Content -Path $files[$i] -Raw
     [string]$output = $null
     $count = 0
     for($j=0;$j -lt $content.Length;$j++){       
         $output += $content[$j]
         if(($content[$j] -eq " ") -or ($content[$j] -eq "`r") -or ($content[$j] -eq "`n")){
             if(-not $whitespace){         
                 $count ++
             }
             if($count%30 -eq 0){
                 $output += "`r`n"
             }
             $whitespace = $true
         }
         else{
             $whitespace = $false
         }
     }
     $output | Out-File -FilePath $results[$i]
 }

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


works super, I change only the

$output += "`r`n"

with

$output += "`r`n`r`n"




0 Votes 0 ·

works, thanks. But if I have 1.000 files, how can I find and replace all ? Because I don't have the time to put to each of them the path


$file = Get-Content C:\temp\file2.txt...etc

and

$output | Out-File C:\temp\result1.txt ...etc


0 Votes 0 ·
Chris-1748 avatar image
0 Votes"
Chris-1748 answered ·

Suzana,
you can also test and try it with split

 $output = $null
 $count = $null
 $file = Get-Content C:\temp\file2.txt
 $array = $file -split(" ")
    
 foreach($word in $array){
     $output += $word
     $count ++
     if($count%30 -eq 0){
         $output += "`r`n"
     }
     else{
         if ($word -ne $array[-1])  # check if last word no space needed
         {
             $output +=" "
         }
     }
 }
 $output | Out-File C:\temp\result1.txt


· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

works, thanks. But if I have 1.000 files, how can I find and replace all ? Because I don't have the time to put to each of them the path


$file = Get-Content C:\temp\file2.txt...etc

and

$output | Out-File C:\temp\result1.txt ...etc


0 Votes 0 ·
Chris-1748 avatar image
0 Votes"
Chris-1748 answered ·

yes, i know. Its only an example ;-)

you need a second foreach loop and get-childitem to read all files


 $allfiles = Get-ChildItem c:\temp\*.txt 
 foreach($onefile in $allfiles){ 
    #your other code 
    $result = "result_" + $onefile.name 
    $outfile | out-file c:\temp\$result 
 } 
· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

can you please update this last code with the first code, as to mark it as an accepted answer ?

I update your code, almost work. It makes the files, but they are empty files:

See this print screen, the result of the code below (with red color): https://snipboard.io/etUa1f.jpg


  $allfiles = Get-ChildItem c:\Folder1\*.html
  foreach($onefile in $allfiles){
  $array = $file -split(" ")
        
  foreach($word in $array){
      $output += $word
      $count ++
      if($count%30 -eq 0){
          $output += "`r`n"
      }
      else{
          if ($word -ne $array[-1])  # check if last word no space needed
          {
              $output +=" "
          }
      }
  }
  $result = "result_" + $onefile.name 
      $output | out-file c:\Folder1\$result 
  }


0 Votes 0 ·
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered ·

Hi @SuzanaEree-2102 ,

You only need to get the file paths at the beginning.

 $filesdir = "C:\temp\files"
 $resultsdir = "C:\temp\results"
 $files = Get-ChildItem -Path $filesdir -File *.txt
 foreach($file in $files){
     $content = Get-Content -Path $file.FullName -Raw
     [string]$output = $null
     $count = 0
     for($j=0;$j -lt $content.Length;$j++){       
         $output += $content[$j]
         if(($content[$j] -eq " ") -or ($content[$j] -eq "`r") -or ($content[$j] -eq "`n")){
             if(-not $whitespace){         
                 $count ++
             }
             if($count%30 -eq 0){
                 $output += "`r`n`r`n"
             }
             $whitespace = $true
         }
         else{
             $whitespace = $false
         }
     }
     $output | Out-File -FilePath $resultsdir\$($file.name)
 }
· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@IanXue-MSFT works, thank you !

But, also, @Chris-1748 have a good idea. Maybe you can correct my code with your update code, because it almost works.

0 Votes 0 ·