CFLib.org – Common Function Library Project

fileSize(filename)

Last updated July 11, 2006
Download UDF

author

Jesse Houwing                                     Jesse Houwing

Version: 3 | Requires: ColdFusion 5 | Library: FileSysLib

 
Rated 0 time(s). Average Rating: 0

Description:
This function will return the length of a file. It uses the standard Java File object, which makes it very fast under ColdfusionMX. If a directory is passed instead of a file, the UDF will return the total size of all files in the directory. If the file or folder does not exist, it will return 0.

Return Values:
Returns a number.

Example:

<cfset filename="c:\autoexec.bat">
<cfoutput>
    Length: #fileSize(filename)#<br>
</cfoutput>

Parameters:

Name Description Required
filename The filename or directory path. Yes

Full UDF Source:

<cfscript>
/**
* This function will return the length of a file or a directory.
* Version 2 by Nathan Dintenfass
* Version 3 by Nat Papovich
*
* @param filename      The filename or directory path. (Required)
* @return Returns a number.
* @author Jesse Houwing (j.houwing@student.utwente.nl)
* @version 3, July 11, 2006
*/

function fileSize(pathToFile) {
    var fileInstance = createObject("java","java.io.File").init(toString(arguments.pathToFile));
    var fileList = "";
    var ii = 0;
    var totalSize = 0;

    //if this is a simple file, just return it's length
    if(fileInstance.isFile()){
     return fileInstance.length();
    }
    else if(fileInstance.isDirectory()) {
        fileList = fileInstance.listFiles();
        for(ii = 1; ii LTE arrayLen(fileList); ii = ii + 1){
         totalSize = totalSize + fileSize(fileList[ii]);
        }
        return totalSize;
    }
    else
        return 0;
}
</cfscript>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
3 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson