Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

lithoart

macrumors newbie
Original poster
Aug 24, 2010
4
1
I use a PHP script that records the number of clicks on a weblink I want to monitor. Every so often I want to zero (or empty) a number of files (all are .dat files) to start the counting afresh. Is there a way to automate this using maybe Automator or AppleScript?
 

NoNameBrand

macrumors 6502
Nov 17, 2005
434
1
Halifax, Canada
I use a PHP script that records the number of clicks on a weblink I want to monitor. Every so often I want to zero (or empty) a number of files (all are .dat files) to start the counting afresh. Is there a way to automate this using maybe Automator or AppleScript?

If the PHP script opens the file for writing, why not just check its age and fopen() it with 'w' instead of 'a' if it's not fresh.
 

lithoart

macrumors newbie
Original poster
Aug 24, 2010
4
1
sorry, I am NEW to this.

What you said doesn't mean anything to me.

Mind elaborating?

Thanks!

If the PHP script opens the file for writing, why not just check its age and fopen() it with 'w' instead of 'a' if it's not fresh.
 

NoNameBrand

macrumors 6502
Nov 17, 2005
434
1
Halifax, Canada
sorry, I am NEW to this.

What you said doesn't mean anything to me.

Mind elaborating?

Thanks!

Sorry— I assumed you meant you'd written the PHP script. There are many ways of doing what you want to do, in PHP or with some other language/tool.

Depending on how your PHP script works, my suggestion might not even work. Open a copy in your favourite text editor and see if it has an 'fopen' command in it.

My thought was that somewhere, the PHP script will open the appropriate .dat file for appending (new stuff written at the end), with an fopen( 'foo.dat', 'a') command:
PHP:
<?php
// this is a comment
// rest of programme
$file = 'foo.dat';
// rest of programme

$fileHandle = fopen($file, 'a');
//programme writes writes writes data and closes file

And instead did something like this, using file_exists() and filemtime():

PHP:
<?php
// this is a comment
// rest of programme
$file = 'foo.dat';
define('MAX_AGE', 7*24*60*60); //allowable age of file, in seconds
// rest of programme

if( fileexists($file) && (date('Ymdhis', filemtime($file) < date('Ymdhis', time() - MAX_AGE)) ) {
  //nukes existing file if it exists and is too old
  $fileHandle = fopen($file, 'w');
} else {
  $fileHandle = fopen($file, 'a');
}
//programme writes writes writes data and closes file

The above works exactly the same, except if the file is too old, in which case it's overwritten with the new information.
 

angelwatt

Moderator emeritus
Aug 16, 2005
7,852
9
USA
If you simply delete the file the script will probably just recreate it giving the effect you want. You write a simple command to delete that file and put it in crontab or just make a runnable script that you can run manually.

From Terminal, this will delete a file.
Code:
rm some/path/to/file
Here's the wikipedia page on cron.

Just depends on how you want to do this and how often.
 

NoNameBrand

macrumors 6502
Nov 17, 2005
434
1
Halifax, Canada
Perhaps deletion isn't even the best option - if you're using 10.5 or 10.6, newsyslog is used by the OS to rotate log files.

One can also install logrotate from MacPorts or Fink.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.