Looper

Having a good looper is a great script to keep available…

#save as 'looper-output.ps1'
# Set $day
$day = Get-Date -Format 'yyyyMMdd'
$filePath = Read-host -prompt "What log do you want to watch? like C:\Robocopy\Robocopy_log"
if(!(test-path $filePath)){continue}
# Initialize a hash table to track seen lines
$seenLines = @{}
while ($true) {
  # Read the last 5 lines from the file
  $lastLines = Get-Content $filePath -Tail 5
  # Process each line
  foreach ($line in $lastLines) {
  if (-not $seenLines.ContainsKey($line)) {
  # Display new line
  Write-Host $line
  $seenLines[$line] = $true
  }
  }
  # Wait for a few seconds before checking again
  Start-Sleep -Seconds 5
}
# Additional cleanup or post-processing code here
Get-ChildItem $filePath -Verbose
<#
Explanation:
This is a prompting looper, so you don;t have to hard code the input.
We use a hash table ($seenLines) to keep track of lines we’ve already seen. For each line in the last 5 lines of the file, we check if it’s already in the hash table. If it’s not in the hash table (i.e., a new line), we display it and add it to the hash table. The loop continues to check for new lines every 5 seconds.
When no new lines are found, the script exits the loop.
This modified script will display only new content, preventing repeated lines.
#>

I wish I could remember where I got this form, but I thank you, Anony-Mouse scripter! [::mouse::]


Posted

in

, ,

by

Tags: