Skip to content

SplitFileByLines (split a text file into chunks)

ts
function SplitFileByLines(
  path:          string,  // required
  linesPerChunk: number,  // required
  outputDir:     string   // required
): number;

NOTE

This function is available as of Syncplify Server! v7.1.1. If you are running an older version, upgrade to v7.1.1 or later to use it.

Splits a text file into multiple smaller files, each containing at most linesPerChunk lines. The output files are written to outputDir and are named after the source file with a zero-padded numeric suffix inserted before the extension — for example, report_0001.csv, report_0002.csv, and so on.

This is useful for breaking up large files before transferring them or feeding them to systems that have row-count limits.

ParameterTypeRequirementExplanation
pathstringrequiredFully qualified path to the source file to split
linesPerChunknumberrequiredMaximum number of lines per output file
outputDirstringrequiredDirectory where the chunk files will be written
Return valueExplanation
number (≥ 1)Number of chunk files created
-1The operation failed (file not found, directory not writable, etc.)

Example

ts
var chunks = SplitFileByLines('/var/data/big_report.csv', 1000, '/var/split/');
if (chunks < 0) {
  Log('Split failed.');
} else {
  Log('Created ' + chunks + ' chunk files.');
}

Given a source file /var/data/big_report.csv with 2500 lines and linesPerChunk = 1000, the output would be:

Output fileLines
big_report_0001.csv1000
big_report_0002.csv1000
big_report_0003.csv500

NOTE

For binary or non-line-oriented files, use SplitFileBySize instead.