Solved

Script or batch to check modified date on file and report

Posted on 2014-11-24
5
2,374 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 28

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 83

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 28

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 83

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 28

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The viewer will learn how to dynamically set the form action using jQuery.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now