File Object and Folder object returned by the GetFile and GetFolder of Javascript FileSystemObject (FSO) provide the access to the Copy method that enables you to copy the specified file or folder to new location. You can copy the specified file or folder to a new location with new name also, using this Copy Method. FSO Copy 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. Copy method also has the feature of overwriting the existing file based on its parameter dedicated for file overwriting permissions. If Copy 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 Copy Method
fileObj.Copy( destination, [overwrite] ); // or folderObj.Copy( destination, [overwrite]);
Above syntax for FSO Copy method shows that it accepts two types of parameters:
1. destination: this parameter accepts the destination path for the specified file where it is to be copied. If a file with same name already exists then it can be controlled with overwrite parameter.
2. overwrite: optional. This parameter accepts value as 0 or 1, true or false. If true then Copy method overwrites the existing file otherwise not. Default value for overwrite parameter is True.
Example 1
File object to execute the Copy Method
<script type="text/javascript">
// initialize ActiveXObject and create an object of Scripting.FileSystemObject.
var fso = new ActiveXObject("Scripting.FileSystemObject");
// object created for file object returned by GetFile method
var fileObj = fso.GetFile("C:\\Temp\\textfile.txt");
fileObj.Copy("C:\\Temp\\copy_of_textfile.txt", true);
</script>
Example 2
Folder object to execute the Copy Method
<script type="text/javascript">
// initialize ActiveXObject and create an object of Scripting.FileSystemObject.
var fso = new ActiveXObject("Scripting.FileSystemObject");
// object created for folder object returned by GetFolder method
var folderObj = fso.GetFolder("C:\\Temp\\myFolder");
folderObj.Copy("C:\\Temp\\copy_of_myFolder", true)
</script>
Continue to next tutorial: Javascript FSO Delete Method to learn how to delete the specified file or folder.