3.
Moving files from Source to Destination
Aim:
To create a [Link] code to move files from source to destination.
Algorithm:
Step 1: Start the process.
Step 2: Use the require module in [Link] to include an external module named fs which is
used to handle the file system operations.
Step 3: Create a variable named oldpath which is used to store the source path from where the
file is to be moved.
Step 4: Create a variable named newpath which is used to store the destination path where the
file is to be moved.
Step 5: Use the rename() method in fs module to move the file from source to destination.
Pass the source and destination paths as parameters to the rename() method.
Step 6: Use the error function to display the message “Error – File not moved” or display the
message “Successfully moved file”.
Step 7: Stop the process.
Coding:
var fs = require('fs')
var oldPath = "D:/New folder/[Link]"
var newPath = "D:/New folder 1/[Link]"
[Link](oldPath, newPath, function (err) {
if (err) {
[Link]("Error - File not moved")
} else {
[Link]("Successfully moved the file from Source folder to Destination folder")
}
})
Output:
Successfully moved the file from Source folder to Destination folder
Error - File not moved