Qbs

Blog Documentation Get Qbs Community

File Service

The File service offers limited access to the file system for operations such as copying or removing files.

Operations that return information about a file have their result tracked, which means the call will be re-run on the next invocation of Qbs and compared to the previous value. If the values differ, the project will be re-resolved (if the call appears on the right-hand side of a property) or, if it is in a rule or command script, that rule or command will be re-run.

Available Operations

canonicalFilePath

File.canonicalFilePath(filePath: string): string

Returns the canonical path of filePath, that is, an absolute path without symbolic links or redundant "." or ".." elements. If the file does not exist, an empty string is returned.

Note: The result of this operation is tracked.

copy

File.copy(sourceFilePath: string, targetFilePath: string): boolean

Copies sourceFilePath to targetFilePath. Any directory components in targetFilePath that do not yet exist will be created. If sourceFilePath is a directory, a recursive copy will be made. If an error occurs, a JavaScript exception will be thrown.

Note: targetFilePath must be the counterpart of sourceFilePath at the new location, not the new parent directory. This allows the copy to have a different name and is true even if sourceFilePath is a directory.

Note: The file is not copied if the source file timestamp is older than the destination file timestamp. If you want to replace the newer file, you need to remove it first via File.remove().

exists

File.exists(filePath: string): boolean

Returns true if and only if there is a file at filePath.

Note: The result of this operation is tracked.

directoryEntries

File.directoryEntries(path: string, filter: File.Filter): string[]

Returns a sorted list of the directory path's contents non-recursively, filtered by filter. The values of filter are equivalent to Qt's QDir::Filter.

Note: The result of this operation is tracked.

lastModified

File.lastModified(filePath: string): number

Returns the time of last modification for the file at filePath. The concrete semantics of the returned value are platform-specific. You should only rely on the property that a smaller value indicates an older timestamp.

Note: The result of this operation is tracked.

makePath

File.makePath(path: string): boolean

Makes the directory at path, creating intermediate directories if necessary. Conceptually equivalent to mkdir -p

move

File.move(oldPath: string, newPath: string, overwrite: boolean = true): boolean

Renames the file oldPath to newPath. Returns true on success and throws an error on failure. If a file with the name newPath already exists and overwrite is false, the file is not overwritten and an error is thrown.

remove

File.remove(filePath: string): boolean

Removes the file at filePath. In case of a directory, it will be removed recursively.