During an AIR training session I held in a company who wanted to evaluate the technology for some of their projects, some developers asked me how to save a file programmatically using a different name each time the file is saved.
My first reply was to use the Date object in order to create a unique string to use as file name. So the code would be something like that :
file = File.documentsDirectory.resolvePath("snapshot" + new Date().time.toString() + ".jpg");
or, a second possibility is to use the DateFormatter class with the format() method to format a Date object and get back a String containing the new, formatted, value :
var file:File = File.createTempDirectory().resolvePath(("snapshot" + dateFormatter.format(new Date())+".jpg");
Any other tecniques to use ?






















It seems that with your solution and a really fast machine, you may in inadvertently end up duplicate file names if you resolve two paths within a millisecond. Not sure how much that needs to be taken into account, but it is a potential issue.
I've handled this type of situation by using a file name with an incrementing value, so in this case, snapshot1.jpg, snapshot2.jpg. To get the usable file name we would need to know if a file exists with the current increment. Not up on AIR and AS3 coding, but using a mix of the AIR API code and some JavaScript syntax, it could be something like:
fileName = "snapshot@@INCREMENT@@.jpg";
increment = 1;
while( ! File.documentsDirectory.resolvePath( fileName.replace(/@@INCREMENT@@/, increment).exists ) ){
increment++;
}
file = File.documentsDirectory.resolvePath( fileName.replace(/@@INCREMENT@@/, increment);
Seems that this could be abstracted easily into a function that takes the parent folder, a base name and the extension to use or even take just the folder path and a full file name and the function could return the full path that is incremented appropriately (even leaving off the increment if the full file name is available for use.
An issue with this might be that if you are using the async file access, then the file path may be available when you ask what file name to use, but not available when you try to write the file out. Again, not sure how big a deal this is, but want to throw that out there as a potential issue.
Posted by: Danilo Celic | February 09, 2008 at 12:56 AM
Hey Danilo,
thank you for point it out !
It's a good point of view....
Hope to talk to you soon,
ciao
Posted by: Marco Casario | February 09, 2008 at 10:38 AM
there are issues as well:
1) What happen if the application is closed and re-run
2) What happen if ther eis already a file with that name
Normally OSes provide a way to generate unique files name for a given directory, unfortunately AIR can't access such capability directrly. One possible solution is to use date and time to build file name, not sure if you can access milliseconds from the Time object.
Posted by: Emanuele Cipolloni | February 09, 2008 at 06:23 PM
You could save a progressive numeric value in a LSO ("snapshot_"): so, when it's time to save, you get the current value, increment it by one, save file "embedding" the incremented value in its name. My two cents... :-)
Posted by: Mauro | February 11, 2008 at 10:55 AM
thank you for your articles
Posted by: para kazan | February 25, 2008 at 05:22 PM