Mike Chambers

code = joy

Monitoring File Changes in Adobe AIR

with 27 comments

I have just uploaded a new class to the as3corelib library that makes it easy to monitor files for changes.

The class is called FileMonitor, and is in the com.adobe.air.filesystem package. Here is a simple example of it in use:

import com.adobe.air.filesystem.FileMonitor;
import flash.filesystem.File;
import flash.events.Event;
import com.adobe.air.filesystem.events.FileMonitorEvent;


private var monitor:FileMonitor;

private function onSelectButtonClick():void
{
	var f:File = File.desktopDirectory;
	f.addEventListener(Event.SELECT, onFileSelect);
	f.browseForOpen("Select a File to Watch.");
}

private function onFileSelect(e:Event):void
{
	var file:File = File(e.target);
	
	if(!monitor)
	{
		monitor = new FileMonitor();
		monitor.addEventListener(FileMonitorEvent.CHANGE, onFileChange);
		monitor.addEventListener(FileMonitorEvent.MOVE, onFileMove);
		monitor.addEventListener(FileMonitorEvent.CREATE, onFileCreate);
	}
	
	monitor.file = file;
	monitor.watch();
}

private function onFileChange(e:FileMonitorEvent):void
{
	trace("file was changed");
}

private function onFileMove(e:FileMonitorEvent):void
{
	trace("file was moved");	
}

private function onFileCreate(e:FileMonitorEvent):void
{
	trace("file was created");
}

 

Note that the class broadcasts three events:

  • FileMonitorEvent.CHANGE : Broadcast when the contents of a monitored file changes. This is currently based on the timestamp of the file.
  • FileMonitorEvent.MOVE : Broadcast when a monitored file is moved or deleted.
  • FileMonitorEvent.CREATE : Broadcast when a monitored file is created (since you can have a File instance that points to a file that does not exist).

The new classes are not yet in the as3corelib builds, but if you want to play with them, you can grab them from the project source.

Note, that I am working on updating the unit tests for the library, so if you run the test runner from source, some of the tests will fail. This should be fixed in a couple of days.

Written by mikechambers

March 11th, 2009 at 10:54 am

Posted in General

27 Responses to 'Monitoring File Changes in Adobe AIR'

Subscribe to comments with RSS or TrackBack to 'Monitoring File Changes in Adobe AIR'.

  1. cool !

    Franck

    11 Mar 09 at 11:43 am

  2. [...] Monitoring File Changes in Adobe AIR Source: Mike Chambers Excerpt: [...]

  3. [...] > Monitoring File Changes in Adobe AIR at Mike Chambers [...]

  4. Mike,

    Very nice work… I will be using your new classes in a logging system that reads log files that are perpetually updated.

    Thanks,

    Jeff Boothe

    Jeff Boothe

    15 Mar 09 at 9:59 pm

  5. Mike,

    I found a typo on in FileMonitorEvent.as on line 16.

    public function FileMonitorEvent(type:String, bubbles:Boolean=false, ancelable:Boolean=false)

    ancelable should be cancelable.

    Thanks,

    Jeff Boothe

    Jeff Boothe

    16 Mar 09 at 8:21 am

  6. [...] For source and example visit Monitor File Changes in Adobe AIR [...]

  7. Hi Mike,

    I built a small AIR app that uses your new filemonitor classes to monitor flashlog.txt. The app works perfectly on OS X, but on XP and Vista the following occurs:

    If my AIR app is running, the flash debug player can not output to the flashlog.txt file. (verified by viewing the logfile manually with or without the Air app running) I think monitoring the file may lock it and prevent trace statements from reaching flashlog.txt on XP and Vista.

    Any suggestions?

    Tom

    Tom Newton

    17 Mar 09 at 3:02 am

  8. re: my earlier comment

    It looks like the AIR app itself is locking the flashlog.txt file. Is there anyway to disable trace output for an AIR app? ( i.e. ensure that AIR doesn’t try to access the mm.cfg file and/or the flashlog.txt file. )

    Cheers,
    Tom

    Tom Newton

    17 Mar 09 at 4:43 am

  9. @jeff


    I found a typo on in FileMonitorEvent.as on line 16.

    Thanks. Ill make sure that get fixed.

    Thanks for the heads up.

    mike chambers

    mesh@adobe.com

    mikechambers

    17 Mar 09 at 9:06 am

  10. @Tom Newton

    Interesting.

    Are you writing to the file? Or just using trace? How often are you tracing?

    mike chambers

    mesh@adobe.com

    mikechambers

    17 Mar 09 at 9:07 am

  11. i did a similar AIR project to watch changes in flash log but it seems that AIR always locks the flashlog txt file and couldnt find any workaround. So i finally decided to use cygwin with a combination of tail and grep command to watch the log , i recommend cygwin is a great tool :)

    paranoio

    17 Mar 09 at 12:16 pm

  12. I have implemented the file watcher in a custom logger application with absolutely no problems.

    Our custom logger swc library writes xml log files and our Air log reader client reads the files during live debugging or closed session logs.

    Works perfectly!

    Thanks, Mike.

    Jeff Boothe

    Jeff Boothe

    18 Mar 09 at 7:39 pm

  13. [...] Monitoring File Changes in Adobe AIR at Mike Chambers Mike Chambers nice Lib for tracking file changes on AIR (tags: air flex actionscript as3 library adobe actionscript3 flash) The Links for 2009-04-11 by David Gamez, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial 2.5 Mexico License. Bookmark to: Hide Sites $$(‘div.d456′).each( function(e) { e.visualEffect(‘slide_up’,{duration:0.5}) }); « Links for 2009-04-10 [...]

  14. @mikechambers,

    I gotta say that i’ve faced same problem with AIR being accessing FlashLog.txt all the time. I have a monitor app that is running on the background constantly, and this cuts the browser’s ability to write traces in that file.

    And yeah, I’m just using trace(). Unfortunately, cannot use Flash’s IDE because it doesn’t know how to open https links correctly.

    Thanks.

    Aleksandr

    16 Apr 09 at 7:00 am

  15. Hi this is great I am using flex to create an air app
    is there a Flex version for this?
    I tried using this buts its not working maybe I am doing something wrong

    Almog

    5 May 09 at 5:12 pm

  16. hi,

    i think there’s a bug in the watch()/unwatch() methods: if you call unwatch() and then watch() again, the timer is not restarted because the listener was removed in the unwatch() method. this could be fixed by setting timer=null at the end of the unwatch() method. further, it’s not clear that watch() has to be called again when a new file is set with the file-setter (because it calls unwatch() internally). otherwise it’s a helpful class, thanks!

    monitor

    22 May 09 at 4:36 am

  17. Now that I have this code I can take over the world and also avoid writing a text editor. Thanks!

    yar

    19 Jul 09 at 3:18 pm

  18. Cheers Mike,
    awesome work! I wanted to make an air app that would allow syncing of your directories on an ftp server using the ftp protocol.

    How intensive is FileMonitor performance wise. I’ve seen some air apps which hog alot of RAM, would monitoring 1000 Files kill your machine?

    noj

    30 Aug 09 at 5:30 pm

  19. damn, never mind. I thought the FileMonitor would allow to monitor a folder, and would let you add events to see what files changed, where they have moved e.t.c

    noj

    30 Aug 09 at 5:47 pm

  20. Anyway to monitor a folder which returns the file name that is being changed? I have used f.browseForDirectory(“select a folder”);
    which works perfectly. However, it does not return the file name of the file that is being changed.

    JC

    5 Oct 09 at 3:17 am

  21. hi–Is there a way to do this without using a Timer do constantly poll for changes? I’d like to see something truly event driven–something that is triggered by a system event, rather than polling every ms to see if there’s been a change–a true listener.

  22. Good day, Mike Chambers!
    First of all, thank you for intersting and usefull article about files monitoring.

    I create sample project, download last .zip archive of as3core library (there is no air.filesystem package, but a manually add neccessary files FileMonitor.as, FileMonitorEvent.as, FileUtil.as, VolumeMonitor.as).

    But, when I run the project – I got exception at
    FileMonitor.as line 135:
    if(timer.running)… // here timer object is null

    So, could you send me sample project?

    Thx

    Alexander

    30 Nov 09 at 7:57 am

  23. This works fine monitoring a folder/directory (dispatches a change event if a file within the folder is either created, removed, or modified)… But it does not seem to recurse into any sub-directories. Is there an easy way to get it to do this? If not I suppose the best thing would be to manually recurse into the directory and add a new monitor for every subdirectory… but that may get resource intensive seeing as this is running off timers.

    Greg

    17 Dec 09 at 2:30 pm

  24. Great help for my project, thanks :P

    IdeasMX

    21 Jan 10 at 2:48 pm

  25. Hi,
    Indeed a great class to work with.I used this class to monitor changes in xml and swf files.When I use to monitor changes in xml file or to say ,it is declared first,it works well.But when I use to monitor changes in swf file no result is shown.
    I am using two objects of FileMonitor class ,one for changes in xml and other for changes in swf.
    So I am getting result of change in xml file only.Am I doing anything incorrectly.
    Thanks in advance

    santosh

    14 Mar 10 at 2:30 am

  26. [...] utility class is inspired by bit101‘s FileMonitor and disappointment by lack of folder monitoring feature. It’s quite [...]

Leave a Reply

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: