-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Moves a file from a source path to a destination path. | ||
* If the destination filename is omitted, the source filename is used. | ||
* | ||
* @param {string} sourceFilePath - The full file path including the filename in the source directory. | ||
* @param {string} destPath - The destination directory path or the full file path including the filename. | ||
* @returns {void} This function does not return a value. | ||
* @throws Will throw an error if the file cannot be moved, e.g., file does not exist, or permissions are insufficient. | ||
*/ | ||
function moveFile(sourceFilePath, destPath) { | ||
var sourceFile = new java.io.File(sourceFilePath); | ||
var destFile; | ||
|
||
// Check if the destination path is a directory or a file path | ||
var dest = new java.io.File(destPath); | ||
if (dest.exists() && dest.isDirectory()) { | ||
// If destPath is a directory, use the source filename in the destination directory | ||
destFile = new java.io.File(dest, sourceFile.getName()); | ||
} else { | ||
// If destPath includes the filename, use it as the destination file path | ||
destFile = dest; | ||
} | ||
|
||
// Move the file to the destination path | ||
org.apache.commons.io.FileUtils.moveFile(sourceFile, destFile); | ||
} | ||
|
||
// Example calls to the moveFile function: | ||
// Moves 'report.pdf' from 'C:/temp/reports' to 'D:/archive/reports' with the filename 'report.pdf' | ||
try { | ||
moveFile("C:/temp/reports/report.pdf", "D:/archive/reports"); | ||
} catch (e) { | ||
console.log("Error occurred during file move: " + e.message); | ||
} | ||
|
||
// Moves 'report.pdf' from 'C:/temp/reports' to 'D:/archive/reports' with a new filename 'archived_report.pdf' | ||
try { | ||
moveFile("C:/temp/reports/report.pdf", "D:/archive/reports/archived_report.pdf"); | ||
} catch (e) { | ||
console.log("Error occurred during file move: " + e.message); | ||
} |