tags:

views:

360

answers:

3

I cannot find a function that tells me the attributes of a given file. I specifically need to get the file's size. How do I find this info.?

edit:

I think I found an answer, just not the answer I was hoping for:

So far till ColdFusion 7, there was no good way to find information like size, last modified date etc about a file. Only way you could do that was to use cfdirectory tag to list the directory, get the query from it, loop over the query until you hit the desired file and then fetch the required metadata. http://coldfused.blogspot.com/2007/07/new-file-io-in-coldfusion-8-part-ii.html

Anyone know of a better way?

+3  A: 
<cffunction name="getFileSize">
    <cfargument name="filepath">

    <cfreturn createObject("java","java.io.File").init(Arguments.filepath).length()>

</cffunction>
RC
I got gibberish...code:<html><head><title>A ColdFusion Page</title></head><body><cffunction name="getFileSize"> <cfargument name="filepath"> <cfreturn createObject("java","java.io.File").init(Arguments.filepath).length()></cffunction><cfset filePath = "C:\richard\anthony\sampleFile.txt"><cfset getFileSize(filePath)><cfoutput> sampleFile.txt is this big: #getFileSize#</cfoutput>output:sampleFile.txt is this big: cftest2ecfm1841964723$funcGETFILESIZE@167b9d4
Anthony
+7  A: 

I believe cfdirectory is your simplest answer - but note, you can use the filter attribute as your filename, and you won't have to loop over the result.

CF Jedi Master
I'd add this code example to your answer for extra awesome: <cfdirectory action="list" filter="myfile.txt" name="fileCheck"/><cfoutput>#fileCheck.size#</cfoutput>
Shawn Grigson
+1  A: 

The CFLib FileSysLibrary has a few functions which can do it for you.

derivation