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