mFile.copyFolderAsynchronously()

Availability

SupportPCMacPackage (AS3) / Scope (AS2)
ActionScript 3.0

4.0 or later

4.0 or later

stm.mprojector.mFile

ActionScript 2.0

4.0 or later

4.0 or later

global

Usage

mFile.copyFolderAsynchronously(sourceFolder:String, destinationFolder:String[, replaceExistingFolder:Boolean]) : Number

Parameters

sourceFolder

destinationFolder

replaceExistingFolder -

  • if true, then any existing folder is overwritten.
  • if false, then the function fails if the folder already exists.
If this parameter is not specified, it will be treated as "false."

Returns

A number.

Description

Method; Copies a folder asynchronously, sending progress information back through either (for AS2) mFile.onCopyFolderProgress() and mFile.onCopyFolderComplete(), or (for AS3) mFileEvent.COPY_FOLDER_PROGRESS and mFileEvent.COPY_FOLDER_COMPLETE

ActionScript 3.0 Example:

This example shows how to use the asynchronous copy functions, add event listeners, and handle events.

//== initialize application ==========
import flash.display.*;
import stm.mprojector.mApplication;
import stm.mprojector.mFile;
import stm.mprojector.mFileEvent;
import stm.mprojector.mSystem;
import fl.controls.TextArea;
import fl.controls.Button;

//== setup copy variables ==========
var sourceFilenameMinusExtension:String = "bigFile";
var delimiter:String = mSystem.getPathDelimiter();
var sourceFolderPath:String = mApplication.getFolder()+delimiter+ "bigFolder";
var sourceFilePath:String = sourceFolderPath+delimiter+ sourceFilenameMinusExtension + ".jpg";
var destinationFilePath:String;
var sessionID:Number;
var iFileCopy:Number = 0;
var iFolderCopy:Number = 0;

//== log events to event_log_txt TextArea
var event_log_txt:TextArea = new TextArea();
event_log_txt.setSize(500, 310);
event_log_txt.move(25, 120);
addChild(event_log_txt)
event_log_txt.text = "intialize application..." + newline;

//== add buttons
copyFile_btn = new Button();
copyFolder_btn = new Button();
abort_btn = new Button();

copyFile_btn.width = 90;
copyFolder_btn.width = 90;
abort_btn.width = 90;

copyFile_btn.move(25,85);
copyFolder_btn.move(125,85);
abort_btn.move(225,85);

copyFile_btn.label = "copy big.jpg";
copyFolder_btn.label = "copy big folder";
abort_btn.label = "abort";

addChild(copyFile_btn);
addChild(copyFolder_btn);
addChild(abort_btn);

//==  start file copy  ==========

function copyFile_btn_click(e:Event):void {
	event_log_txt.text += "copying file..." + newline;
	iFileCopy ++;
	// copy big.jpg to big-1.jpg
	destinationFilePath = sourceFolderPath + delimiter + sourceFilenameMinusExtension + "-" + iFileCopy +".jpg";
	sessionID = mFile.copyFileAsynchronously(sourceFilePath, destinationFilePath, true);
	if (sessionID) {
		event_log_txt.text += "copy sessionID..." +sessionID+ newline;
	} else {
		event_log_txt.text += "copy file async failed"+ newline;
	}
}
copyFile_btn.addEventListener(MouseEvent.CLICK, copyFile_btn_click);

//== start folder copy  ==========

function copyFolder_btn_click(e:Event):void {
	event_log_txt.text += "copying file..." + newline;
	iFolderCopy++; 
	// copy bigFolder to bigFolder-1
	destinationFolderPath = sourceFolderPath + "-" + iFolderCopy;
	sessionID = mFile.copyFolderAsynchronously(sourceFolderPath, destinationFolderPath, true);
	if (sessionID) {
		event_log_txt.text += "copy sessionID..." +sessionID+ newline;
	} else {
		event_log_txt.text += "copy folder async failed"+ newline;
	}
}
copyFolder_btn.addEventListener(MouseEvent.CLICK, copyFolder_btn_click);

//== handle mApplication.abort() cancels all async processes including async folder and file copies ==========

function abort_btn_click(e:Event):void {
	event_log_txt.text += "aborting..." + newline;
	mApplication.abortFunction(sessionID);
}
abort_btn.addEventListener(MouseEvent.CLICK, abort_btn_click);

//== ============= Load the image we just copied ==================

function loadJPG():void {
	event_log_txt.text += "Begin loading file..." + destinationFilePath+ newline;

	var ldr:Loader = new Loader();
	ldr.scaleX = 0.125;
	ldr.scaleY = 0.125;
	ldr.x = 423;
	ldr.y = 23;
	//ldr.mask = rect;
	//var url:String = "/temp/3.jpg";
	var urlReq:URLRequest = new URLRequest(destinationFilePath);
	ldr.load(urlReq);
	event_log_txt.text += "loading file..." + destinationFilePath+ newline;

	addChild(ldr);
}

//==================== Trace out copy events to text area ==========

function fileEventHandler(e:mFileEvent):void {
	switch (e.type) {
		case mFileEvent.COPY_FILE_PROGRESS :
			event_log_txt.text += new Date().toTimeString() + ": mFile.copyFile " + e.type + " sessionID: " +  e.sessionID.toString()  + " read: " + e.read.toString() + " total: "  + e.total.toString()  + newline;
			if (e.total == e.read) {
				if (e.read > 0) {
					event_log_txt.text +=  "copyFile: e.total == e.read" + newline;
				} else {
					// read = total = 0 means copyFile failed
					event_log_txt.text +=  "copyFile failed" + newline;
				}
			}
			break;
		case mFileEvent.COPY_FILE_COMPLETE :
			event_log_txt.text += new Date().toTimeString() + ": mFile.copyFile " + e.type + newline;
			if (e.success) {
				event_log_txt.text += "copy file successful" + newline;
				loadJPG();
			} else {
				event_log_txt.text += "copy file failed " + newline;
			}
			break;
		case mFileEvent.COPY_FOLDER_PROGRESS :
			event_log_txt.text += new Date().toTimeString() + ": mFile.copyFolder " + e.type + " sessionID: " +  e.sessionID.toString()  + " read: " + e.read.toString() + " total: "  + e.total.toString()  + newline;
			if (e.total == e.read) {
				if (e.read > 0) {
					event_log_txt.text +=  "copyFolder: e.total == e.read" + newline;
				} else {
					// read = total = 0 means copyFile failed
					event_log_txt.text +=  "copyFolder failed" + newline;
				}
			}
			break;
		case mFileEvent.COPY_FOLDER_COMPLETE :
			event_log_txt.text += new Date().toTimeString() + ": mFolder.copyFolder " + e.type + newline;
			if (e.success) {
				event_log_txt.text += "copy folder successful" + newline;
			} else {
				event_log_txt.text += "copy folder failed" + newline;
			}
			break;
		default :
			event_log_txt.text += new Date().toTimeString() + ": mFile " + e.type + newline;
	}
}


//==================== initialize mProjector event handlers ==========

function initListeners():void {
	event_log_txt.text += "initListeners" + newline;

	mFile.addEventListener(mFileEvent.COPY_FILE_PROGRESS, fileEventHandler);
	mFile.addEventListener(mFileEvent.COPY_FILE_COMPLETE, fileEventHandler);
	mFile.addEventListener(mFileEvent.COPY_FOLDER_PROGRESS, fileEventHandler);
	mFile.addEventListener(mFileEvent.COPY_FOLDER_COMPLETE, fileEventHandler);
}
initListeners();

ActionScript 2.0 Example:

This example shows how to use the asynchronous copy functions, add event listeners, and handle events.

//== setup copy variables ==========
var delimiter:String = mSystem.getPathDelimiter();
var sourceFolderPath:String = mApplication.getFolder()+delimiter+ "bigFolder";
var destinationFilePath:String;
var sessionID:Number;
var iFolderCopy:Number = 0;

//== log events to event_log_txt TextArea
this.createClassObject(mx.controls.TextArea, "event_log_txt", 109, {text:""});
event_log_txt.setSize(500, 310);
event_log_txt.move(25, 120);
event_log_txt.text = "intialize application..." + newline;

//== add buttons
this.createClassObject(mx.controls.Button, "copyFolder_btn", 111, {label:"copy big folder"});
this.createClassObject(mx.controls.Button, "abort_btn", 112, {label:"abort copy"});

copyFolder_btn.width = 90;
abort_btn.width = 90;

copyFile_btn.move(25,85);
abort_btn.move(225,85);


//== start folder copy  ==========

function copyFolder_btn_click() {
	event_log_txt.text += "copying file..." + newline;
	iFolderCopy++; 
	// copy bigFolder to bigFolder-1
	destinationFolderPath = sourceFolderPath + "-" + iFolderCopy;
	sessionID = mFile.copyFolderAsynchronously(sourceFolderPath, destinationFolderPath, true);
	if (sessionID) {
		event_log_txt.text += "copy sessionID..." +sessionID+ newline;
	} else {
		event_log_txt.text += "copy folder async failed"+ newline;
	}
}
copyFolder_btn.addEventListener("click", copyFolder_btn_click);

//== handle mApplication.abort() cancels all async processes including async folder and file copies ==========

function abort_btn_click() {
	event_log_txt.text += "aborting..." + newline;
	mApplication.abortFunction(sessionID);
}
abort_btn.addEventListener("click", abort_btn_click);

//== ============= Load the image we just copied ==================

function loadJPG() {
	// load jpg
}

//==================== Trace out copy events to text area ==========

mFile.onCopyFolderProgress = function(sessionID, read, total) {
	event_log_txt.text += Date().toString() + ": copyFolder " + type + " sessionID: " +  sessionID  + " read: " + read + " total: "  + total  + newline;
	if (total == read) {
		if (read > 0) {
			event_log_txt.text +=  "copyFolder: total == read" + newline;
		} else {
			// read = total = 0 means copyFile failed
			event_log_txt.text +=  "copyFolder failed" + newline;
		}
	}

}
mFile.onCopyFolderComplete = function(sessionID, read, total) {
	event_log_txt.text += Date().toString() + ": copyFolder " + newline;
	if (success) {
		event_log_txt.text += "copy folder successful" + newline;
	} else {
		event_log_txt.text += "copy folder failed" + newline;
	}
}

See Also

mFile.onCopyFolderProgress(), mFile.onCopyFolderComplete(), mFileEvent.COPY_FOLDER_PROGRESS, mFileEvent.COPY_FOLDER_COMPLETE, mFile.copyFileAsynchronously()

Code Examples

CopyFileAsync