Monitoring File Changes in Adobe AIR
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.






cool !
Franck
11 Mar 09 at 11:43 am
[...] Monitoring File Changes in Adobe AIR Source: Mike Chambers Excerpt: [...]
Monitoring File Changes in Adobe AIR | Switch on the Code
11 Mar 09 at 12:10 pm
[...] Mike Chambers – AIR – http://www.mikechambers.com/blog/2009/03/11/monitoring-file-changes-in-adobe-air/ [...]
12/03/2009 « Robertopriz’s Weblog
12 Mar 09 at 6:36 am
[...] > Monitoring File Changes in Adobe AIR at Mike Chambers [...]
localToGlobal » Blog Archive » news review -> 11th week of 2009
13 Mar 09 at 10:01 am
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
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
[...] For source and example visit Monitor File Changes in Adobe AIR [...]
Monitor File Changes in Adobe AIR | Adobe AIR Tutorials
17 Mar 09 at 1:47 am
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
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
@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
@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
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
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
[...] 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 [...]
Links for 2009-04-11 at .swfgeek
11 Apr 09 at 8:02 am
@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
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
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
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
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
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
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
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.
charlie crystle
3 Nov 09 at 10:30 am
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
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
Great help for my project, thanks :P
IdeasMX
21 Jan 10 at 2:48 pm