– Common Function Library Project


Last updated July 11, 2006

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

Rated 0 time(s). Average Rating: 0

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.


view plain print about
<cfset filename="c:\autoexec.bat">
    Length: #fileSize(filename)#<br>


Name Description Required
filename The filename or directory path. Yes

Full UDF Source:

view plain print about
 * 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 ( 
 * @version 3, July 11, 2006 

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

    //if this is a simple file, just return it's length
        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; 
        return 0;
blog comments powered by Disqus


Latest Additions

CF Ninja CF Ninja added
14 day(s) ago

Stephen Withington Stephen Withington added
14 day(s) ago

Adam Cameron Adam Cameron added
a while ago

Ray Ford Ray Ford added
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 48 time(s)

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 9 time(s)

Darwan Leonardo Sitepu splitNumber
Rated 5.0, 8 time(s)

Created by Raymond Camden / Design by Justin Johnson