Empty contents of a file

Discussion in 'Mac Apps and Mac App Store' started by lithoart, Aug 24, 2010.

  1. lithoart macrumors newbie

    Joined:
    Aug 24, 2010
    #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?
     
  2. NoNameBrand macrumors 6502

    Joined:
    Nov 17, 2005
    Location:
    Halifax, Canada
    #2
    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.
     
  3. lithoart thread starter macrumors newbie

    Joined:
    Aug 24, 2010
    #3
    sorry, I am NEW to this.

    What you said doesn't mean anything to me.

    Mind elaborating?

    Thanks!

     
  4. NoNameBrand macrumors 6502

    Joined:
    Nov 17, 2005
    Location:
    Halifax, Canada
    #4
    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.
     
  5. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
    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.
     
  6. NoNameBrand macrumors 6502

    Joined:
    Nov 17, 2005
    Location:
    Halifax, Canada
    #6
    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.
     

Share This Page