File Object and Folder object returned by the GetFile and GetFolder of Javascript FileSystemObject (FSO) provide the access to the Delete method that enables you to delete the specified file from the client’s PC. Javascript FSO Delete method could perform its action only if the security permissions of web browser at client’s end allow your web page to create the FSO object via Javascript Automation server and access the file system to delete the specified file. If file exits at the specified location then Delete method deletes the file otherwise throws an error.
Syntax for FSO Delete Method
fileObj.Delete( [force] ); // or folderObj.Delete( [force] );
Above syntax for FSO Delete method shows that it accepts only one optional parameter:Force: optional. This parameter accepts Boolean value i.e. 0/1 or true or false. If true then Delete method will delete the read-only files otherwise not. By Default its value is false. Delete method could not delete read-only files.
Example 1
File object to execute the Delete 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\\myFolder\\copy_of_textfile.txt");
fileObj.Delete(true);
</script>
Example 2
Folder object to execute the Delete 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\\copy_of_myFolder");
folderObj.Delete(true)
</script>
Continue to next tutorial: Javascript FSO Move Method to learn how to move a specified file or folder to the specified location.