AndyJarrett

cffile in cfscript quick cheat sheet

First time for using CFFILE within cfscript so thought I should document this for myself and anyone else.

Open and close a file

myFile = expandPath( "somefile.txt" ); fileObj = fileRead( myFile ); writeOutput(fileObj); // ORmyFile = expandPath( "somefile.txt" ); fileObj = fileOpen( myFile, "read" ); fileClose( fileObj ); // Once a file is closed you cannot access the object

Write to a file

myFile = expandPath( "somefile.txt" ); data = "I'm going to create a file object"; FileWrite( "fileObj", data ); newFileObj = FileRead( "fileObj" ); writeDump(var=newFileObj); // OR write direct to filemyFile = expandPath( "somefile.txt" ); data = "I'm going to write to direct to file"; FileWrite(myFile, data);

Append a line to a file

myFile = expandPath( "somefile.txt" ); fileObj = FileOpen( myFile, "append"); fileWriteLine(fileObj,"This line is new."); fileClose(fileObj);

Output a text file to the screen

myFile = expandPath( "somefile.txt" ); fileObj = fileOpen( myFile, "read" ); while( NOT fileIsEOF( fileObj ) ){ line = fileReadLine( fileObj ); WriteOutput( "#line#" ); } FileClose( fileObj );

Copy/Move and delete a file

myFile = expandPath( "somefile.txt" ); myCopyFile = expandPath( "somefile2.txt" ); fileCopy( myFile, myCopyFile); // fileMove() works the samewriteOutput( fileExists( myCopyFile ) ); // Check copy workedfileDelete( myCopyFile ); writeOutput( fileExists( myCopyFile ) ); // Check delete worked