Utility to monitor folder for a line in a text file

Soldato
Joined
27 Nov 2004
Posts
10,338
Location
North Beds
Hey,

I need a utility that monitors a folder that has text files outputted from a program into it.

I need it to look for a certain value in said text files, and if it exists, replace it with another.

Is this something that can be done easily or am I being a little optimistic?

Thanks,

tom.
 
On Linux, "grep -r mypattern ~/logs/*.txt" will try and find "mypattern" in all of the .txt files in ~/logs.

Then you could use `sed` to replace the value.
 
In PowerShell you would use something like this:

Code:
$dir = "C:\tmp\*.txt"
$files = DIR $Dir

foreach ($file in $files)

{
	(Get-Content $file) | % {$_ -replace "lol", "moo"} | Set-Content $file
}

Replaces the word "lol" with "moo" on all lines in all .txt files in directory C:\tmp\
 
Eulogy, that looks along the lines of what I want.

This is my first time using powershell for a script. How would I go around using this and setting a timer for example so that it runs once a minute or whatever? It's windows server 2008.

Thanks for your help guys :)
 
right, ok sorted it, needed a slight tweak to the code

Code:
$dir = "C:\tmp\*.txt"
$files = DIR $Dir

foreach ($file in $files)

{
	(Get-Content $file) |Foreach-Object {$_ -replace "lol", "moo"} |Set-Content $file
}

It needed the foreach-ojbect to work properly

I've saved this as a ps1 file and created a shortcut powershell.exe c:\test.ps1 and set this as a scheduled action to run once every 5 minutes. Is this the most efficient way?

Thanks again,

Tom.
 
You would probably just use task scheduler to run it at regular intervals.

As you appear to be using Windows with the interest in PowerShell, you could also code up a little utility in .NET that uses the FileSystemWatcher to monitor specific folder and notify automatically when things change rather than constantly polling.

It's a cleaner solution, but may be overkill for your needs!
 
You would probably just use task scheduler to run it at regular intervals.

As you appear to be using Windows with the interest in PowerShell, you could also code up a little utility in .NET that uses the FileSystemWatcher to monitor specific folder and notify automatically when things change rather than constantly polling.

It's a cleaner solution, but may be overkill for your needs!

the folder is only going to ever have say 5 or 6 text files in it at any point on a bad day, and each only about 20 lines long.

Is there a way of noting what changes it makes and the file name in which it makes the changes, and outputting it to a log file? So you can look back in say a month and see how many times it's actually actioned, and when and what filename?

thanks,

Tom.
 
the folder is only going to ever have say 5 or 6 text files in it at any point on a bad day, and each only about 20 lines long.

Is there a way of noting what changes it makes and the file name in which it makes the changes, and outputting it to a log file? So you can look back in say a month and see how many times it's actually actioned, and when and what filename?

thanks,

Tom.

The FileSystemWatcher can raise events whenever things change so you can easily output to a log (or database) how many times particular files have changed over a period.
If you want to find out exactly what has changed in the file you'd need to do a bit more work as you'd have to manually put something together to get the data in the file once it's changed and compare it to the previous data (which you'd need to store somewhere)

It's obviously a bit more work if you want something to handle all of that.
 
try this:

Code:
$dir = "C:\tmp\*.txt"
$files = DIR $Dir
$logfile = "C:\tmp\log.txt"
$output = ""

Clear-Content $logfile

foreach ($file in $files)

{
$newline = @()
$count = 0

$filecontent = (Get-Content $file)

	foreach ($line in $filecontent)

	{
	
		if ($line -match "lol")
		{
		$count++
		}

	$newline = $newline + ($line -replace "lol", "cat")
	$newline | Set-Content $file

	}

	if ($count -ne 0)
	{
	$date = Get-Date
	$output = "$file changed on $date"
	Write-Host $output
	$output | Out-File $logfile -append
	}

}
 
Eulogy, that works except the next time it runs, it replaces the log.txt instead of appending to it. I've removed the Clear-Content $logfile and it seems ok now :)

One note, is it possible for it to show what was changed in the file and how many times? The script is going to have a few criteria ie "X to Y" | "A to B" and it'd be nice to know which ones it had changed.

for reference this is the code atm:

Code:
$dir = "C:\tmp\*"
$files = DIR $Dir
$logfile = "C:\tmp\log.txt"
$output = ""

Clear-Content $logfile


foreach ($file in $files)

{
$newline = @()
$count = 0

$filecontent = (Get-Content $file)

	foreach ($line in $filecontent)

	{
	
		if ($line -match "lol")
		{
		$count++
		}

		if ($line -match "rofl")
		{
		$count++
		}




	$newline = $newline + ($line -replace "lol", "cat" -replace "rofl", "coptor")
	$newline | Set-Content $file

	}

	if ($count -ne 0)
	{
	$date = Get-Date
	$output = "$file changed on $date"
	Write-Host $output 
	$output | Out-File $logfile -append
	}

}

One other note is that the log file has spaces between all the characters, ie :

Code:
C : \ t m p \ t e s t . t x t   c h a n g e d   o n   0 3 / 2 3 / 2 0 1 1   1 0 : 0 5 : 4 8

This is only a very minor thing though and if it's not a simple switch then don't worry
 
Back
Top Bottom