Javascript FileSystemObject (FSO) provides access to the CopyFile method that performs the action of copying a File from one location to another location according to the specified path. FSO CopyFile method works only if the client’s web browse allows creating the object for Scripting.FileSystemObject as well as provides the access to the file system of the client’s PC. CopyFile method also has the feature of overwriting the existing file based on its parameter dedicated for file overwriting permissions. If CopyFile method of Javascript FSO throws an error of File overwrite permissions denied then ask the user to enable the Javascript and activeX permissions.
Syntax for Javascript FSO CopyFile Method
fso.CopyFile(source, destination, [overwrite]);
Above syntax for FSO CopyFile shows that it accepts three types of parameters:
1. source: this parameter accepts the path of the source file that is to be copied to some other location.
2. destination: this parameter accepts the destination path for the source file where it is to be copied. If a file with same name already exists then it can be controlled with overwrite parameter.
3. overwrite: optional. This parameter accepts value as 0 or 1, true or false. If true then CopyFile method overwrites the existing file otherwise not. Default value for overwrite parameter is True.
Example for Javascript FSO CopyFile Method
<script type="text/javascript">
// initialize ActiveXObject and create an object of Scripting.FileSystemObject.
var fso = new ActiveXObject("Scripting.FileSystemObject");
// copies a file to another location + overwrites the file if already exits
fso.CopyFile("C:\\Temp\\folder1\\demo.txt", "C:\\Temp\\folder1\\test.txt", 1);
// copies a file to another location + does not overwrite the file if already exits
fso.CopyFile("C:\\Temp\\folder1\\demo.txt", "C:\\Temp\\folder1\\test.txt", 0);
fso = null;
</script>
Continue to next tutorial: Javascript FSO CopyFolder Method to learn copying a folder from one location to another.