This will accomplish what you need, I changed the way text files are being parsed as $file = Get-Content $files
feels...wrong. Using a for loop we create a reference point $i
which can be used to look ahead in the array $content
.
The second part of -and
statement - (($i + 1) -lt $content.Count
- makes sure you don't get OOB exception if you were to look beyond the "edge" of the $content
array, ie when looking at the last line ( $i = $content.Count - 1 ).
$files = Get-ChildItem $source *.*
foreach($file in $files){
$content = Get-Content $file
for($i = 0; $i -lt $content.Count; $i++){
$line = $content[$i]
if ($line.StartsWith("6")) {
Write-Host "This line starts with 6"
} elseif ($line.StartsWith("4")) {
Write-Host "This line starts with 4"
} elseif (($line.StartsWith("4")) -and (($i + 1) -lt $content.Count)) {
$nextLine = $content[$i+1]
if($nextLine.StartsWith("4")){
Write-Host "This line starts with 4 and the next line starts with 4"
}
} else {
Write-Host "This line does not start with 6 or 4"
}
}
}
Hope this helps.