Monday, March 22, 2010    
Home My Books Blog ColdFusion About Me Back    

Calendar
<< May 2007 >>
S M T W T F S
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

Search

Categories
 • Acrobat (3) [RSS]
 • Adobe (90) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (66) [RSS]
 • AdobeMAX09 (39) [RSS]
 • AdobeMAX10 (1) [RSS]
 • AIR (219) [RSS]
 • Appearances (191) [RSS]
 • Books (72) [RSS]
 • CFEclipse (15) [RSS]
 • ColdFusion (1382) [RSS]
 • Data Services (34) [RSS]
 • Fish Tank (5) [RSS]
 • Flash (198) [RSS]
 • Flex (499) [RSS]
 • Home Automation (5) [RSS]
 • Jobs (116) [RSS]
 • JRun (14) [RSS]
 • Labs (43) [RSS]
 • LiveCycle (34) [RSS]
 • MAX (232) [RSS]
 • Mobile (120) [RSS]
 • Regular Expressions (17) [RSS]
 • RIA (21) [RSS]
 • SQL (40) [RSS]
 • Stuff (536) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (162) [RSS]

Other BLOGs
 • Charlie Arehart
 • Lee Brimelow
 • Ray Camden
 • Christophe Coenraets
 • Sean Corfield
 • Mihai Corlan
 • Cornel Creanga
 • Mark Doherty
 • John Dowdell
 • Danny Dura
 • Enrique Duvos
 • Steven Erat
 • Kevin Hoyt
 • Serge Jespers
 • Adam Lehman
 • Duane Nickull
 • Miti Pricope
 • Andrew Shorten
 • Ryan Stewart
 • James Ward
 • Greg Wilson
 • Full As A Goog

RSS Feeds
 • Feed
 • Subscribe

Join my mailing list and find out about new books and other topics of interest.

Thoughts, ideas, tips, musings, and pontifications (not necessarily in that order) by Ben Forta ...
NOTE: This is my personal blog, and the opinions and statements voiced here are my own.

Viewing By Day : May 11, 2007 / Main
May 11, 2007

Scorpio File I/O Enhancements

At one of the usergroup sessions this week someone asked if there was a way to get file information (size, date time, etc.) easily using a function. I said they should use <CFDIRECTORY>, but afterwards remembered that we did indeed add a new function to Scorpio called GetFileInfo() which returns a structure containing:

  • canread
  • canwrite
  • ishidden
  • lastmodified
  • name
  • parent
  • path
  • size
  • type

Which makes this a good opportunity to review some of the file i/o changes coming in Scorpio.

For starters, if you have ever had to work with large text files in ColdFusion (maybe parsing a large CSV file) you'll know that doing so is very inefficient. You probably use code like this:

<!--- Read entire file --->
<cffile action="read"
    file="#fileName#"
    variable="myFile">

<!--- Loop through file variable one line at a time --->
<cfloop list="#myFile#"
    index="line"
    delimiters="#chr(10)##chr(13)#">

    <!--- Do stuff with line here --->
    ...
</cfloop>

This is slow for two reasons. Not only does ColdFusion read the entire file into memory in a variable all at once, but also looping through the file requires treating it as a list which involves lots of parsing which can also be resource intensive.

Well, inefficient no more. In ColdFusion Scorpio you'll be able to replace the above code block with this:

<!--- Loop through file one line at a time --->
<cfloop file="#fileName#"
    index="line">

    <!--- Do stuff with line here --->
    ...
</cfloop>

This code block open the file, reads one line at a time, and closes it when done. I actually used this myself recently in a ColdFusion code snippet that had to parse a massive tab delimited file, turning each line into a query row. Replacing the old <CFFILE> <CFLOOP> with a new <CFFILE FILE=> cut down processing time from several minutes to under 10 seconds.

Oh, and although reading files line by line is the more common use case, you can also read by n characters at a time, like this:

<!--- Loop through file 100 characters at a time --->
<cfloop file="#fileName#"
    index="chars"
    characters="100">

    <!--- Do stuff with line here --->
    ...
</cfloop>

In addition to the <CFLOOP> enhancements, we've also added lots of new file i/o functions that you can use to access and manipulate files directly. The new functions include:

  • FileClose()
  • FileCopy()
  • FileDelete()
  • FileIsEOF()
  • FileMove()
  • FileOpen()
  • FileRead()
  • FileReadBinary()
  • FileReadLine()
  • FileSetAccessMode()
  • FileSetAttribute()
  • FileSetLastModified()
  • FileWrite()
  • GetFileInfo()
  • IsImageFile()
  • IsPDFFile()

TrackBacks
Faster file parsing in Scorpio
Finally! File parsing will get faster in Coldfusion, as Ben Forta informs us via his blog.
Tracked by Build It Smarter | Tracked on 5/14/07 12:33 PM

No trackback URL. Trackbacks are only allowed via interactive form.

Comments

  © Copyright 1997-2009 Ben Forta, All Rights Reserved