Javascript FileSystemObject (FSO) consists of FileExists method that provides the functionality to check whether the specified file exists at the given location or not. FSO FileExists method returns the Boolean value i.e. true or false based on the existence of the specified file name. FileExists method returns true if the file exits at the specified location and returns false if file or location does not exist. Javascript FileExists method is very useful while creating or deleting the files at client side coz if file does not exist while deletion action then it throws an error and stops the execution of rest of javascript code placed after that code line.
Syntax for Javascript FSO FileExists Method
fso.FileExists( fileSpec );
As shown above FSO FileExists method accepts only one parameter as file name along with its path. It checks the file at the specified location and returns the Boolean value accordingly.
Example for FSO FileExists Method
<script type="text/javascript">
// initialize ActiveXObject and create an object of Scripting.FileSystemObject.
var fso = new ActiveXObject("Scripting.FileSystemObject");
// if condition to check whether the specified file exists or not.
if (fso.FileExists("C:\\Temp\\myFolder\\file.txt")) {
document.write("File.txt exists.");
}
else {
document.write("File.txt does not exist.");
}
fso = null;
</script>
Continue to next tutorial: Javascript FSO FolderExists Method to learn about checking whether the specified directory exists or not.