JavaScript move file

This page discusses - JavaScript move file

JavaScript move file

JavaScript move file

        

In this section, we are going to move a file from one folder to another using JavaScript.

In the given example, firstly we have created an ActiveXObject object which is used to enable and return a reference to an Automation object. It takes the parameter 'servername' which represents the name of the application and 'typename' represents the type or class of object to create. Then, we have used the JavaScript method GetFile() with this object which returns the file with the specified path. To move the file from the specified location to another location, we have used JavaScript Move() method. This method takes a single parameter, destination, which represents the location to which the file is to be moved.

Here is the code:

<html>
<h2>Move file in JavaScript</h2>
<script>
function moveFile(){
var object = new ActiveXObject("Scripting.FileSystemObject");
var file = object.GetFile("C:\\My\\Hello.txt");
file.Move("C:\\MyFile\\");
document.write("File is moved successfully");
}
</script>
<form>
<input type="Button" value="Move File" onClick='moveFile()'>
</form>
</html>

When you load the above html code, you will get a button. On clicking the button, you will find that the file is moved to the destination path.

Download Source Code: