12

EDIT: I'm trying to read all the files in a specific folder and list the files in there, not read the content of a specific file. I just tried to simply create an FileSystemObject and it doesn't do anything either. I show an alert (which pops up) beforfe making the FileSystemObject, and one after it (which isn't shown). So the problem is in simply creating the object.

Original:

I am trying to read all the files in a folder by using JavaScript.

It is a local HTML file, and it will not be on a server, so I can't use PHP I guess.

Now I'm trying to read all the files in a specific given folder, but it doesn't do anything on the point I make a FileSystemObject

Here is the code I use, The alert shows until 2, then it stops.

 alert('1');
 var myObject, afolder, date;
 alert('2');
 myObject = new ActiveXObject("Scripting.FileSystemObject");
 alert('3');
 afolder = myObject.GetFolder("c:\\tmp");
 alert('4');
 date = afolder.DateLastAccessed;
 alert("The folder"+name+" is a temporary folder.");

Am I doing this the right way?

Thanks!

asked Aug 11, 2011 at 7:04
6
  • What does the error console of your browser say? Commented Aug 11, 2011 at 7:07
  • It doesn't say anything, it just doesn't do anything anymore after it pops up with '2'. Is there a way I can debug this? Commented Aug 11, 2011 at 7:11
  • Sorry, Just debugged it with firebug. The error says ActiveXObject is not defined Commented Aug 11, 2011 at 7:39
  • 2
    You say "firebug", I hear Firefox. Firefox does not know about ActiveXObject, because it's a proprietary Microsoft technology. Commented Aug 11, 2011 at 19:01
  • It's not clear if the specific folder is selected by a user or predefined in the code. If it's the first case (and folder selection dialog is ok for you) there is a simple html5 solution. Commented Nov 30, 2016 at 8:35

2 Answers 2

4

The method I found with a Google search uses HTML5 so if you are using a modern browser you should be good. Also the tutorial page seems to check if the browser you are using supports the features. If so you should be good to follow the tutorial which seems pretty thorough.

http://www.html5rocks.com/en/tutorials/file/dndfiles/

answered Aug 11, 2011 at 7:08
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer, but I'm looking to read the files in a folder as in list all the files that are in there, not read the content of the files. I just tried to create an object of the filesystemobject, and it doesn't do anything either. It doesn't seem to work here. I just want to list all the files that are in a given folder.
4

This solution only works on IE11 or older since it is MS based

<script type="text/javascript"> 
 var fso = new ActiveXObject("Scripting.FileSystemObject"); 
 function showFolderFileList(folderspec) { 
 var s = "";
 var f = fso.GetFolder(folderspec);
 // recurse subfolders
 var subfolders = new Enumerator(f.SubFolders);
 for(; !subfolders.atEnd(); subfolders.moveNext()) {
 s += ShowFolderFileList((subfolders.item()).path);
 } 
 // display all file path names.
 var fc = new Enumerator(f.files);
 for (; !fc.atEnd(); fc.moveNext()) {
 s += fc.item() + "<br>";
 }
 return s; 
 }
 function listFiles() {
 document.getElementById('files').innerHTML = showFolderFileList('C:');
 }
</script>
<input type='button' onclick='listFiles()' value='List Files' />
<div id="files" />
answered Aug 19, 2015 at 15:45

3 Comments

When I try this I get the error "ActiveXObject is not defined".
Hi Addem, this solution only works on IE11 or older since it is MS based. can you confirm that's what you're using?
@BelgoCanadian could you add your comment to your answer

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.