datasource.renameFile ^v2.7.191
Renames or moves a file within the MODLR file system. Both the source and destination paths must be within the root directory of the datasource.
js
datasource.renameFile(fileName, newName, overwrite)Parameters
fileName- The current name and file path of the file in the MODLR file system. File path can be found in the filesystem page.newName- The new name and file path for the file.overwrite(optional) - Iftrue, overwrites the target file if it already exists. Iffalseor omitted, returns an error if the target file exists. Defaults tofalse.
Returns
boolean- Returnstrueif the file was renamed successfully,falseif:- The source file path is invalid
- The source file does not exist
- The new file path is invalid
- The target file already exists (when
overwriteisfalse) - The target file could not be deleted (when
overwriteistrue) - The rename operation failed
Example
js
// Simple rename in the same directory
datasource.renameFile("report.txt", "report_final.txt");
// Move a file to a different directory
datasource.renameFile("input/data.csv", "archive/data_2024.csv");
// Rename and overwrite if the target exists
datasource.renameFile("temp/output.json", "results/output.json", true);
// Check if rename was successful
let success = datasource.renameFile("old.txt", "new.txt");
if (!success) {
console.log("Failed to rename file");
}