FLfile object

 < Day Day Up > 

Availability

Flash MX 2004 7.2.

Description

The FLfile object lets you write Flash extensions that can access, modify, and remove files and folders on the local file system. The FLfile API is provided in the form of an extension to the JavaScript API. This extension is called a shared library and is located in the following folder:

  • Windows 2000 or Windows XP:

    boot drive\Documents and Settings\user\Local Settings\Application Data\Macromedia\Flash 8\ language\Configuration\External Libraries\FLfile.dll

  • Mac OS X:

    Macintosh HD/Users/userName/Library/Application Support/Macromedia/Flash 8/language/Configuration/External Libraries/FLfile.dll

NOTE

Don't confuse the shared libraries that contain symbols in your Flash documents with the JavaScript API shared libraries. They are two different things.


The FLfile methods work with files or folders (directories) on disk. Therefore, each method takes one or more parameters that specifies the location of a file or folder. The location of the file or folder is expressed as a string in a form very similar to a website URL. It is called a file URI (Uniform Resource Identifier) and is formatted as shown below (including the quote marks):

"file:///drive|/folder 1/folder 2/.../filename"

For example, if you want to create a folder on the C drive called config and place it in the Program Files/MyApp folder, use the following command:

FLfile.createFolder("file:///C|/Program Files/MyApp/config");

If you then want to place a file called config.ini in that folder, use the following command:

FLfile.write("file:///C|/Program Files/MyApp/config/config.ini", "");

To create a folder on the Macintosh, you could use the following command:

FLfile.createFolder("file:///Macintosh/MyApp/config");

Method summary for the FLfile object

The following methods can be used with the FLfile object.

Method

Description

FLfile.copy()

Copies a file.

FLfile.createFolder()

Creates one or more folders.

FLfile.exists()

Determines the existence of a file or folder.

FLfile.getAttributes()

Finds out if a file is writable, read-only, hidden, visible, or a system folder.

FLfile.getCreationDate()

Specifies how many seconds have passed between January 1, 1970, and the time the file or folder was created.

FLfile.getCreationDateObj()

Gets the date a file or folder was created.

FLfile.getModificationDate()

Specifies how many seconds have passed between January 1, 1970, and the time the file or folder was last modified.

FLfile.getModificationDateObj()

Gets the date a file or folder was last modified.

FLfile.getSize()

Gets the size of a file.

FLfile.listFolder()

Lists the contents of a folder.

FLfile.read()

Reads the contents of a file.

FLfile.remove()

Deletes a file or folder.

FLfile.setAttributes()

Makes a file or folder read-only, writable, hidden or visible.

FLfile.write()

Creates, writes to, or appends to a file.


FLfile.copy()

Availability

Flash MX 2004 7.2.

Usage

FLfile.copy( fileURI, copyURI )

Parameters

fileURI A string, expressed as a file:/// URI, that specifies the file you want to copy.

copyURI A string, expressed as a file:/// URI, that specifies the location and name of the copied file.

Returns

A Boolean value of TRue if successful; false otherwise.

Description

Method; copies a file from one location to another. This method returns false if copyURI already exists.

Example

The following example makes a backup copy of a configuration file named config.ini and places it inside the same folder in which it is located, with a new name.

var originalFileURI="file:///C|/Program Files/MyApp/config.ini"; var newFileURI="file:///C|/Program Files/MyApp/config_backup.ini"; Flfile.copy(originalFileURI, newFileURI);

If you prefer, you can perform the same task with a single command:

Flfile.copy("file:///C|:/Program Files/MyApp/config.ini",   file:///C|/Program Files/MyApp/config_backup.ini");

FLfile.createFolder()

Availability

Flash MX 2004 7.2.

Usage

FLfile.createFolder( folderURI )

Parameters

folderURI A folder URI that specifies the folder structure you want to create.

Returns

A Boolean value of TRue if successful; false if folderURI already exists.

Description

Method; creates one or more folders at the specified location.

You can create multiple folders at one time. For example, the following command creates both the MyData and the TempData folders if they don't already exist:

FLfile.createFolder("file:///c|/MyData/TempData")

Example

The following example creates two subfolders under the configuration folder (fl.configURI).

fl.trace(FLfile.createFolder(fl.configURI+"folder01/subfolder01"));

The following example attempts to create a folder called tempFolder at the root level on the C drive and displays an alert box indicating whether the operation was successful.

var folderURI = "file:///c|/tempFolder"; if (FLfile.createFolder(folderURI)) {   alert("Created " + folderURI); } else {   alert(folderURI + " already exists"); }

See also

FLfile.remove(), FLfile.write()

FLfile.exists()

Availability

Flash MX 2004 7.2.

Usage

FLfile.exists( fileURI )

Parameters

fileURI A string, expressed as a file:/// URI, that specifies the file you want to verify.

Returns

A Boolean value of true if successful; false otherwise.

Description

Method; determines whether a specified file exists.

Examples

The following example checks for a file called mydata.txt and displays an alert box indicating whether the file exists.

var fileURI = "file:///c|/temp/mydata.txt"; if (FLfile.exists(fileURI)) {   alert( fileURI + " exists!"); } else {   alert( fileURI + " does not exist."); }

The following example checks to see if a required configuration file exists. If the file doesn't exist, it is created:

var configFile = "file:///C|/MyApplication/config.ini"; if (!FLfile.exists(configFile)) {      FLfile.write(configFile,"") }

See also

FLfile.write()

FLfile.getAttributes()

Availability

Flash MX 2004 7.2.

Usage

FLfile.getAttributes( fileOrFolderURI )

Parameters

fileOrFolderURI A string, expressed as a file:/// URI, specifying the file or folder whose attributes you want to retrieve.

Returns

A string that represents the attributes of the specified file or folder.

NOTE

Results are unpredictable if the file or folder doesn't exist. You should use FLfile.exists() before using this method.


Description

Method; returns a string representing the attributes of the specified file or folder, or an empty string if the file has no specific attributes (that it, it is not read-only, not hidden, and so on). You should always use FLfile.exists() to test for the existence of a file or folder before using this method.

Characters in the string represent the attributes as follows:

R fileOrFolderURI is read-only

D fileOrFolderURI is a folder (directory)

H fileOrFolderURI is hidden (Windows only)

S fileOrFolderURI is a system file or folder (Windows only)

A fileOrFolderURI is ready for archiving (Windows only)

For example, if fileOrFolderURI is a hidden folder, the string returned is "DH".

Example

The following example gets the attributes of the file mydata.txt and displays an alert box if the file is read-only.

var URI = "file:///c|/temp/mydata.txt"; if (FLfile.exists(URI)){   var attr = FLfile.getAttributes(URI);     if (attr && (attr.indexOf("R") != -1)) { // Returned string contains R.       alert(URI + " is read only!");   } }

See also

FLfile.setAttributes()

FLfile.getCreationDate()

Availability

Flash MX 2004 7.2.

Usage

FLfile.getCreationDate(fileOrFolderURI)

Parameters

fileOrFolderURI A string, expressed as a file:/// URI, specifying the file or folder whose creation date and time you want to retrieve as a hexadecimal string.

Returns

A string containing a hexadecimal number that represents the number of seconds that have elapsed between January 1, 1970, and the time the file or folder was created, or "00000000" if the file or folder doesn't exist.

Description

Method; specifies how many seconds have passed between January 1, 1970, and the time the file or folder was created. This method is used primarily to compare the creation or modification dates of files or folders.

Example

The following example determines whether a file has been modified since it was created.

// Make sure the specified file exists. var fileURI = "file:///C|/MyApplication/MyApp.fla"; var creationTime = FLfile.getCreationDate(fileURI) var modificationTime = FLfile.getModificationDate(fileURI) if ( modificationTime > creationTime ) {      alert("The file has been modified since it was created") } else {      alert("The file has not been modified since it was created") }

See also

FLfile.getCreationDateObj(), FLfile.getModificationDate()

FLfile.getCreationDateObj()

Availability

Flash MX 2004 7.2.

Usage

FLfile.getCreationDateObj(fileOrFolderURI)

Parameters

fileOrFolderURI A string, expressed as a file:/// URI, specifying the file or folder whose creation date and time you want to retrieve as a JavaScript Date object.

Returns

A JavaScript Date object that represents the date and time when the specified file or folder was created. If the file doesn't exist, the object contains information indicating that the file or folder was created at midnight GMT on December 31, 1969.

Description

Method; returns a JavaScript Date object that represents the date and time when the specified file or folder was created.

Example

The following example displays (in human-readable form) the date a file was created in the Output panel:

// Make sure the specified file exists. var file1Date = FLfile.getCreationDateObj("file:///c|/temp/file1.txt"); fl.trace(file1Date);

See also

FLfile.getCreationDate(), FLfile.getModificationDateObj()

FLfile.getModificationDate()

Availability

Flash MX 2004 7.2.

Usage

FLfile.getModificationDate(fileOrFolderURI)

Parameters

fileOrFolderURI A string, expressed as a file:/// URI, specifying the file whose modification date and time you want to retrieve as a hexadecimal string.

Returns

A string containing a hexadecimal number that represents the number of seconds that have elapsed between January 1, 1970, and the time the file or folder was last modified, or "00000000" if the file doesn't exist.

Description

Method; specifies how many seconds have passed between January 1, 1970, and the time the file or folder was last modified. This method is used primarily to compare the creation or modification dates of files or folders.

Example

The following example compares the modification dates of two files and determines which of the two was modified most recently:

// Make sure the specified files exist. file1 = "file:///C|/MyApplication/MyApp.fla" file2 = "file:///C|/MyApplication/MyApp.as" modificationTime1 = FLfile.getModificationDate(file1) modificationTime2 = FLfile.getModificationDate(file2) if(modificationTime1 > modificationTime2) {      alert("File 2 is older than File 1") } else if(modificationTime1 < modificationTime2) {      alert("File 1 is older than File 2") } else {      alert("File 1 and File 2 were saved at the same time") }

See also

FLfile.getCreationDate(), FLfile.getModificationDateObj()

FLfile.getModificationDateObj()

Availability

Flash MX 2004 7.2.

Usage

FLfile.getModificationDateObj(fileOrFolderURI)

Parameters

fileOrFolderURI A string, expressed as a file:/// URI, specifying the file or folder whose modification date and time you want to retrieve as a JavaScript Date object.

Returns

A JavaScript Date object that represents the date and time when the specified file or folder was last modified. If the file or folder doesn't exist, the object contains information indicating that the file or folder was created at midnight GMT on December 31, 1969.

Description

Method; returns a JavaScript Date object that represents the date and time when the specified file or folder was last modified.

Example

The following example displays (in human-readable form) the date a file was last modified in the Output panel:

// Make sure the specified file exists. var file1Date = FLfile.getModificationDateObj("file:///c|/temp/file1.txt"); trace(file1Date);

See also

FLfile.getCreationDateObj(), FLfile.getModificationDate()

FLfile.getSize()

Availability

Flash MX 2004 7.2.

Usage

FLfile.getSize( fileURI )

Parameters

fileURI A string, expressed as a file:/// URI, specifying the file whose size you want to retrieve.

Returns

An integer that represents the size of the specified file, in bytes, or 0 if the file doesn't exist.

Description

Method; returns an integer that represents the size of the specified file, in bytes, or 0 if the file doesn't exist. If the return value is 0, you can use FLfile.exists() to determine whether the file is a zero-byte file or if the file doesn't exist.

Example

The following example stores the size of the mydata.txt file in the fileSize variable:

var URL = "file:///c|/temp/mydata.txt"; var fileSize = FLfile.getSize(URL);

FLfile.listFolder()

Availability

Flash MX 2004 7.2.

Usage

FLfile.listFolder( folderURI [, filesOrDirectories ] )

Parameters

folderURI A string, expressed as a file:/// URI, specifying the folder whose contents you want to retrieve. You can include a wildcard mask as part of folderURI. Valid wildcards are * (matches one or more characters) and ? (matches a single character).

filesOrDirectories An optional string that specifies whether to return only filenames or only folder (directory) names. If omitted, both filenames and folder names are returned. Acceptable values are "files" and "directories".

Returns

An array of strings representing the contents of the folder, or false if the folder doesn't exist.

Description

Method; returns an array of strings that represent the contents of the folder, or an empty array if the folder doesn't exist.

Examples

The following example returns an array representing the files, folders, or both files and folders in the Program Files directory.

var folderURI = "file:///C|/WINDOWS/Program Files" ; var fileList = FLfile.listFolder(folderURI, "files") // files var fileList = FLfile.listFolder("folderURI", "directories") //folders var fileList = FLfile.listFolder(folderURI) //files and folders

The following example returns an array of all the text (.txt) files in the temp folder and displays the list in an alert box.

var folderURI = "file:///c|/temp"; var fileMask = "*.txt"; var list = FLfile.listFolder(folderURI + "/" + fileMask, "files"); if (list) {   alert(folderURI + " contains: " + list.join(" ")); }

The following example uses a file mask in the specified folderURI to return the names of all the executable files in the Windows application folder:

var executables = FLfile.listFolder("file:///C|/WINDOWS/*.exe","files") alert(executables.join("\n"))

FLfile.read()

Availability

Flash MX 2004 7.2.

Usage

FFLfile.read()

Parameters

fileOrFolderURI A string, expressed as a file:/// URI, specifying the file or folder whose attributes you want to retrieve.

Returns

The contents of the specified file as a string, or null if the read fails.

Description

Method; returns the contents of the specified file as a string, or null if the read fails.

Examples

The following example reads the file mydata.txt and, if successful, displays an alert box with the contents of the file.

var fileURI = "file:///c|/temp/mydata.txt"; var str = FLfile.read( fileURI); if (str) {   alert( fileURL + " contains: " + str); }

The following example reads the ActionScript code from a class file and stores it in the code variable:

var classFileURI = "file:///C|/MyApplication/TextCarousel.as"; var code = Flfile.read(classFileURI);

FLfile.remove()

Availability

Flash MX 2004 7.2.

Usage

FLfile.remove( fileOrFolderURI )

Parameters

fileOrFolderURI A string, expressed as a file:/// URI, specifying the file or folder you want to remove (delete).

Returns

A Boolean value of true if successful; false otherwise.

Description

Method; deletes the specified file or folder. If the folder contains files, those files will be deleted as well. Files with the R (read-only) attribute cannot be removed.

Examples

The following example warns a user if a file exists and then deletes it if the user chooses to do so.

var fileURI = prompt ("Enter file/folder to be deleted: ", "file:///c|/temp/   delete.txt"); if (FLfile.exists(fileURI)) {   var confirm = prompt("File exists. Delete it? (y/n)", "y");   if (confirm == "y" || confirm == "Y") {     if(FLfile.remove(fileURI)) {       alert(fileURI + " is deleted.");     }     else {       alert("fail to delete " + fileURI);     }   } } else {   alert(fileURI + " does not exist"); }

The following example deletes a configuration file created by an application:

if(FLfile.remove("file:///C|/MyApplication/config.ini")) {      alert("Configuration file deleted") }

The following example deletes the Configuration folder and its contents:

FLfile.remove("file:///C|/MyApplication/Configuration/")

See also

FLfile.createFolder(), FLfile.getAttributes()

FLfile.setAttributes()

Availability

Flash MX 2004 7.2.

Usage

FLfile.setAttributes( fileURI, strAttrs )

Parameters

fileURI A string, expressed as a file:/// URI, specifying the file whose attributes you want to set.

strAttrs A string specifying values for the attribute(s) you want to set. For acceptable values for strAttrs, see the description below.

Returns

A Boolean value of TRue if successful.

NOTE

Results are unpredictable if the file or folder doesn't exist. You should use FLfile.exists() before using this method.


Description

Method; specifies system-level attributes for the specified file.

The following values are valid for strAttrs:

N No specific attributes (not read-only, not hidden, and so on)

A Ready for archiving (Windows only)

R Read-only (on the Macintosh, read-only means "locked")

W Writable (overrides R)

H Hidden (Windows only)

V Visible (overrides H, Windows only)

If you include both R and W in strAttrs, the R is ignored and the file is set as writable. Similarly, if you pass H and V, the H is ignored and the file is set as visible.

If you want to make sure the archive attribute is not set, use this command with the N parameter before setting attributes. That is, there is no direct counterpart to A that turns off the archive attribute.

Examples

The following example sets the file mydata.txt to be read-only and hidden. It has no effect on the archive attribute.

var URI = "file:///c|/temp/mydata.txt"; if (FLfile.exists(URI)) {   FLfile.setAttributes(URI, "RH"); }

The following example sets the file mydata.txt to be read-only and hidden. It also ensures that the archive attribute is not set.

var URI = "file:///c|/temp/mydata.txt"; if (FLfile.exists(URI)) {   FLfile.setAttributes(URI, "N");   FLfile.setAttributes(URI, "RH"); }

See also

FLfile.getAttributes()

FLfile.write()

Availability

Flash MX 2004 7.2.

Usage

FLfile.write( fileURI, textToWrite, [ , strAppendMode ] )

Parameters

fileURI A string, expressed as a file:/// URI, specifying the file to which you want to write.

textToWrite A string representing the text you want to place in the file.

strAppendMode An optional string with the value "append", which specifies that you want to append textToWrite to the existing file. If omitted, fileURI is overwritten with textToWrite.

Returns

A Boolean value of TRue if successful; false otherwise.

Description

Method; writes the specified string to the specified file (as UTF-8). If the specified file does not exist, it is created. However, the folder in which you are placing the file must exist before you use this method. To create folders, use FLfile.createFolder().

Example

The following example attempts to write the string "xxx" to the file mydata.txt and displays an alert message if the write succeeded. It then attempts to append the string "aaa" to the file and displays a second alert message if the write succeeded. After executing this script, the file mydata.txt will contain only the text "xxxaaa".

var URI = "file:///c|/temp/mydata.txt"; if (FLfile.write(URI, "xxx")) {   alert("Wrote xxx to " + URI); } if (FLfile.write(URI, "aaa", "append")) {   alert("Appended aaa to " + fileURI); }

See also

FLfile.createFolder(), FLfile.exists()

     < Day Day Up > 


    Developing Extensions for Macromedia Flash 8
    Developing Extensions for Macromedia Flash 8
    ISBN: 032139416X
    EAN: 2147483647
    Year: 2005
    Pages: 81

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net