Skip to content

Instantly share code, notes, and snippets.

@mjclemente
Created April 23, 2021 00:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjclemente/0c88b567801db63e6126e3a986ed5d0c to your computer and use it in GitHub Desktop.
Save mjclemente/0c88b567801db63e6126e3a986ed5d0c to your computer and use it in GitHub Desktop.
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