Skip to content

SplitFileBySize (split a file into fixed-size chunks)

ts
function SplitFileBySize(
  path:          string,  // required
  bytesPerChunk: 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 file into multiple smaller files, each containing at most bytesPerChunk bytes. 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, archive_0001.bin, archive_0002.bin, and so on.

This is useful for splitting large files prior to transfer over systems that impose size limits, such as email attachments or certain FTP servers.

ParameterTypeRequirementExplanation
pathstringrequiredFully qualified path to the source file to split
bytesPerChunknumberrequiredMaximum size of each output file, in bytes
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
// Split a 150 MB file into 50 MB pieces
var MB = 50 * 1024 * 1024;
var chunks = SplitFileBySize('/var/backup/archive.tar', MB, '/var/staging/');
if (chunks < 0) {
  Log('Split failed.');
} else {
  Log('Created ' + chunks + ' chunk files.');
}

NOTE

To split text files by line count rather than byte count, use SplitFileByLines.