Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Script or batch to check modified date on file and report

Posted on 2014-11-24
5
Medium Priority
?
3,330 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 85

Accepted Solution

by:
oBdA earned 1200 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 800 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

916 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