Skip to content

Instantly share code, notes, and snippets.

@mjclemente

mjclemente/filereadlines.cfc Secret

Created Apr 23, 2021
Embed
What would you like to do?
Read a range of file lines in ColdFusion
/**
*
* Based on a UDF by Raymond Camden (https://cflib.org/udf/FileRead)
*
* @hint Read a range of lines from a file
* @filepath path to the file being read
* @start number of the first line to read
* @end number of the last line to read
*/
public string function fileReadLines( required string filepath, required numeric start, required numeric end ) {
if( !fileExists(filepath) ){
return "";
}
var fileLines = '';
var fileObject = fileOpen(filepath);
var done = false;
var lineNumber = 0;
try {
while( !done ) {
lineNumber++;
var line = fileReadLine(fileObject);
if( lineNumber >= start ){
fileLines &= line;
if( lineNumber < end ){
fileLines &= Chr(13) & Chr(10);
}
}
if( lineNumber == end ){
done = true;
}
}
} catch( any e ) {
rethrow;
} finally {
fileClose( fileObject );
}
return fileLines;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment