Solved

Script or batch to check modified date on file and report

Posted on 2014-11-24
5
2,713 Views
Last Modified: 2014-12-08
I have an XML file that gets updated every couple of minutes.  I am unable to tell when it stops updating unless I check the modified date.  So I need a script or batch that can check the modified date every 10 mins to make sure that the modified that has changed since its last scan.  If it has not, then it executes a blat command to send me an email (I have all of the blat commands).  It would need to stay in a loop, pausing for 10 minutes between checks.
0
Comment
Question by:murryc
  • 3
  • 2
5 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40462589
simple powershell  option


function fchecker {
$time = (Get-Date).AddMinutes(10)
$ftouch = gi getline.ps1 | select -expa LastWriteTime
if ($ftouch -lt $time)
{Run email program }
Start-sleep 10
fchecker
}

fchecker

Open in new window

0
 
LVL 84

Accepted Solution

by:
oBdA earned 300 total points
ID: 40462603
Plain batch:
@echo off
setlocal
set File=C:\Temp\test.xml
set Interval=600
for %%a in ("%File%") do set FileName=%%~nxa
set OldFileTime=X
:Loop
for %%a in ("%File%") do set NewFileTime=%%~ta
echo [%Date%][%Time%] '%FileName%' last modified: %NewFileTime%
ping.exe -n %Interval% localhost >NUL
if not "%OldFileTime%"=="%NewFileTime%" (
	set OldFileTime=%NewFileTime%
	goto :Loop
)
echo [%Date%][%Time%] '%FileName%' has not been modified for %Interval% seconds!
ECHO blat.exe ...
goto Loop

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 40468781
Here is an updated version including the mail send piece (simply update the values)

function fchecker {
$time = (Get-Date).AddMinutes(-10)
$ftouch = get-item file.xml | select -expa LastWriteTime
if ($ftouch -lt $time)
{
$Data = "File.xml not updated since $time"
  Send-MailMessage -From 'email@email.com' -To 'email@email.com'
  -SmtpServer 'servername.local' `
  -Subject 'File not Updated ' `
  -BodyAsHtml `
  -Body ($Data -join "<br>") 
}
Start-sleep 600
fchecker
}
fchecker

Open in new window


This will run continuously and check every ten minutes (You can probably set as a scheduled task)
0
 
LVL 84

Expert Comment

by:oBdA
ID: 40469028
becraig,
this will die after about 16 hours in PS1, after about a week in PS2.0, and after probably around three to six weeks in PS3.0 or later, while continuously using more memory, because the function is recursively calling itself.
0
 
LVL 29

Assisted Solution

by:becraig
becraig earned 200 total points
ID: 40469065
To be frank I think a scheduled task is the best way to run this since windows will manage this.

It will take no additional resources to do so, though we can use garbage collection to clean up.

Best implementation in my mind would be:

1. Save the below as a script <script.ps1>
$time = (Get-Date).AddMinutes(-10)
$ftouch = get-item file.xml | select -expa LastWriteTime
if ($ftouch -lt $time)
{
$Data = "File.xml not updated since $time"
  Send-MailMessage -From 'email@email.com' -To 'email@email.com'
  -SmtpServer 'servername.local' `
  -Subject 'File not Updated ' `
  -BodyAsHtml `
  -Body ($Data -join "<br>") 
}

Open in new window


2. Create a scheduled task to check every ten minutes (requires no user interaction once created).
schtasks /create /sc minute /mo 10 /tn "XML File Checker" /tr C:\path-to-script\script.ps1

Open in new window

0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question