Kirix Support Forums

Script to check if a file changes

Please post any help questions, requests or other feedback here.

Script to check if a file changes

Postby Aaron on Thu Jan 13, 2011 3:06 pm

Recently, we had a question about how to monitor a file to see if it changes. Here's a small script that shows how to do this:

Code: Select all

use strict semicolons;
use strict variables;


// file to check
var file_name = "c:\\sample.txt";

// initialize the file info
var file = new FileInfo(file_name);
var file_modified = file.getLastWriteTime();
var file_size = file.getLength();
var file_modified_last = file_modified;
var file_size_last = file_size;


Console.writeLine("Starting...");


// check the file
var count = 0;
while (1)
{
    // wait 1/2 second between checks
    System.sleep(500);
    count++;

    // get the current modified time and file size
    file_modified = file.getLastWriteTime();
    file_size = file.getLength();

    // limit the number of checks
    if (count > 120)
        break;

    // if the file hasn't changed, do nothing
    if (file_modified_last == file_modified ||
        file_size_last == file_size)
    {
        continue;
    }

    // the file has changed
    Console.writeLine(file_name + " changed.");

    // reset modified time and file size to last values
    file_modified_last = file_modified;
    file_size_last = file_size;
}


Console.writeLine("Finished.");
Aaron Williams
Kirix Support Team
User avatar
Aaron
Kirix Support Team
 
Posts: 120
Joined: Fri Dec 16, 2005 3:01 pm

Return to Strata Help & Feedback