Skip to content

Commit

Permalink
Create movefile.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pacmano1 authored Nov 30, 2023
1 parent 7c8b2c3 commit 2074233
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions movefile.js
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);
}

0 comments on commit 2074233

Please sign in to comment.