tags:

views:

48

answers:

2

Hi,

How to check whether a folder exists or not using Javascript?

Thanks, Karthick

+6  A: 

If this is Javascript running within a web browser, you can't. For (extremely valid) security reasons, Javascript in a web page does not have access to the client filesystem.

If for some reason you wanted to check from a web page whether a folder existed on the server, you could make an Ajax call from Javascript to a server method (e.g. Java, PHP, ASP.NET, etc.) that could (depending on permissions) have access to the server filesystem.

If you are using Javascript as a Windows scripting environment, Microsoft provides a FileSystemObject that includes a FolderExists() method.

JacobM
+1  A: 

If you mean a folder on your server, you can make an ajax request to it and check the status code:

var req = new XMLHttpRequest();

req.onreadystatechange = function(){

    if (req.readyState < 4) return;

    if (req.status === 200) alert("The folder exists!");
    else alert("The folder does not exist or can not be accessed!");

}

req.open("GET", "/path/to/folder", true);
req.send("");
Vincent
<script language="JScript"> <!-- function checkfolder() { var myObject; myObject = new ActiveXObject("Scripting.FileSystemObject"); if(myObject.FolderExists("c:\\tmp")){ alert("tmp Folder Exists"); } else { alert("tmp Folder doesn't exist"); } } --> </script> <form name="myForm"> <input type="Button" value="Check Folder" onClick='checkfolder()'> </form>Every time i run this i get activex control message? Is there any way to supress it or any other approach to check a file?
Karthick