Solved

Script or batch to check modified date on file and report

Posted on 2014-11-24
5
3,011 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 85

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 85

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

632 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