-
-
Save mjclemente/0c88b567801db63e6126e3a986ed5d0c to your computer and use it in GitHub Desktop.
Read a range of file lines in ColdFusion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* 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