Skip to content

WaitForFile (wait until a file appears)

ts
function WaitForFile(
  path:            string,  // required
  timeoutMs:       number,  // required
  pollIntervalMs?: number   // optional, default 500
): boolean;

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.

Blocks script execution until the file at path exists on the local file system, or until timeoutMs milliseconds have elapsed. Returns true if the file appeared, false if the function timed out before the file was detected.

This function is useful when a script must wait for an upstream process (such as an FTP upload or a file-drop agent) to finish writing a file before processing it.

ParameterTypeRequirementExplanation
pathstringrequiredFully qualified path of the file to wait for
timeoutMsnumberrequiredMaximum time to wait, in milliseconds
pollIntervalMsnumberoptionalHow often to check for the file, in milliseconds. Default: 500
Return valueExplanation
trueThe file appeared before the timeout expired
falseThe timeout expired and the file still does not exist

Example

ts
// Wait up to 60 seconds for an upload to arrive, checking every second
if (!WaitForFile('/var/inbox/daily_export.csv', 60000, 1000)) {
  Log('Timed out waiting for file.');
} else {
  Log('File arrived, starting processing.');
  // ... process the file
}

TIP

After WaitForFile returns true, the file exists but may still be open for writing. Use FileAgeSecs to verify that the modification time has stopped advancing before processing the file.