Issue
I want to add a new blank line after every 50 words on several text files. I made this code, almost works, it create the new file with the same name, but doesn't make the parsing with the changes from main files. Can anyone give me a little help?
$allfiles = Get-ChildItem c:\Folder1\*.txt
foreach($onefile in $allfiles){
$array = $file -split(" ")
foreach($word in $array){
$output += $word
$count ++
if($count%50 -eq 0){
$output += "`r`n`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
}
Solution
$allfiles = Get-ChildItem c:\Folder1\*.txt
foreach($onefile in $allfiles)
{
$result=[system.collections.generic.list[string]]::new()
$words=(cat $onefile) -split '\s'
$z=49
for($i=0;$i -lt $words.count;$i+=50)
{
$result.Add(($words[$i..$z] -join ' ').trim())
$result.Add("`r`n")
$z=$z+50
}
$result | out-file "c:\Folder1\result_$($onefile.name).txt"
}
Answered By - Santiago Squarzon Answer Checked By - Clifford M. (WPSolving Volunteer)