2

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>
Sparky
  • 98,165
  • 25
  • 199
  • 285
Anwar Ahmat
  • 219
  • 5
  • 14
  • Define "not working". What errors do you get? Which lines fail? – Quentin Nov 23 '15 at 16:43
  • What browsers are you testing on? – Quentin Nov 23 '15 at 16:43
  • You shouldn't be able to access the users' PC from the browser.. Maybe this is possible in some specific cases, but AFAIK this is not possible like this. You could go around using the file system API in HTML5 -> http://stackoverflow.com/questions/13624450/how-to-get-the-contents-of-the-directory-from-local-pc-in-javascript – Rob Nov 23 '15 at 16:45
  • @Quentin alert didn't return any message.I tried both in internet explorer and firefox. – Anwar Ahmat Nov 23 '15 at 16:45
  • @AnwarAhmad — You need to do some basic debugging. Go through it line by line until something fails. You also haven't said which browsers you are testing on yet. – Quentin Nov 23 '15 at 16:46
  • @Quentin I did debugging with alert(FOo). when I put alert (FOo) before "var fs = new ActiveXObject("Scripting.FileSystemObject");" , it shows me the folder name; after "new ActiveXObject " line , it didn't show me any message. – Anwar Ahmat Nov 23 '15 at 16:52
  • 1
    Do you know how to use the Console in the browser's developer tools? There is no way that wouldn't show an error message in Firefox. (I suspect IE would show one as well). – Quentin Nov 23 '15 at 16:55

1 Answers1

0

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.

Sudipta Kumar Maiti
  • 1,669
  • 1
  • 12
  • 18