Solved

Script or batch to check modified date on file and report

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

939 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

4 Experts available now in Live!

Get 1:1 Help Now