Search code examples
javascripthtmlactivexobjectfilesystemobject

how to get all files names and sub folder names in a folder in javascript+html?


I want to get all the file names and sub folders names in JavaScript. But, in my code the activeX object method is not working. Could you find the problem in my code please?

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Report Interface</title>
        <script type="text/javascript">
    function FindFile(FOo)
            {
                var fs = new ActiveXObject("Scripting.FileSystemObject");
                var Folder = fs.GetFolder(FOo);

                var FileCollection = Folder.Files;
                var Files = new Array();

                for(var objEnum = new Enumerator(FileCollection); !objEnum.atEnd(); objEnum.moveNext())
                {
                   strFileName = objEnum.item();
                   alert(strFileName);
                }
            }
        </script>
    </head>
    <body>
        <script type="text/javascript">
            window.onload = function()
            {
                var file_path = "C:/xampp/tomcat/webapps/brt-example/report"; // Starting directory
                FindFile(file_path);
            }
        </script>    
    </body>
</html>

Solution

  • Only IE has support for ActiveX, it can't be accessed with default security settings due to security reasons, if you have to run this -

    Go to IE Tools > Internet options > Security > Custom Level > Under the ActiveX controls and plug-ins, select\ enable for Initializing and Script ActiveX controls not marked as safe for scripting.