Getting file sizes

Posted on 2012-08-30
Last Modified: 2012-08-30
I need to loop through a bunch of directories and look at specific files and get their current filesize.
Using <cfdirectory> wont work because I don't need to get the filesize of entire directories, just specific files that I am determining in my loop.  The files and their paths are being pulled from a database query.
I looked into <cffile> but the read attribute pulls the entire file into memory.

What is a good way to do this?

Question by:WebStalkers
    LVL 51

    Accepted Solution

    All depends on how many files and where they're located.

    If it's a few files OR they're scattered across many different directories,  you could look them up individually.  Run your db query. Loop hrough it and use GetFileInfo() on each file:

                <cfloop query="yourDBQuery">
                            <cfset info = GetFileInfo( fullPathToTheFile )>
                            <cfset sizeInBytes = info.size>
                             ... do something

    If the files are mostly in the same directory, it could save time to use cfdirectory. Do one listing, then loop through it to find the sizes of the files you need.  Don't forget you can use "filters" (ie on file name, extension, ...)

               <cfdirectory filter="*.jpg" ....>  
               <cfdirectory filter="*.xls|*.png" ....>

    Either of these would work. The most efficient approach really depends on your files.
    LVL 2

    Author Comment

    I just happened to find this function.  I've never used it before..very nice!

    LVL 51

    Expert Comment

    Using <cfdirectory> wont work because I don't need to get the filesize
            of entire directories, just specific files that I am determining in my loop.  

    One thing I forgot to clarify.  cfdirectory doesn't calculate the sizes of directories afaik - only files.  For directories it just returns 0.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    Title # Comments Views Activity
    Sum/Avg of results from a CFSCRIPT 1 49
    Entering multiple email addresses 4 51
    cfpdfform and populate 14 52
    cfchart issue with html 6 70
    PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
    I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now