Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dynamically creating list of files in a folder

Posted on 2007-11-28
3
Medium Priority
?
3,377 Views
Last Modified: 2013-11-19
Hi Experts.

I am trying to show the files in a folder that is selected then when the user makes a selection generate a list of files in a listbox if the selection is a folder. When I click ion the 'show selected' button, The status bar tells me there is an error on the page. For testing purpose I call a function to display a message on the click event.

What am I doing wrong?
<HTML>
<HEAD>
<TITLE>"List of Folders"</TITLE>
</HEAD>
 
<script LANGUAGE="JavaScript">
 
doc = "";
 
<!----- Function ListOfFilesInAFolder - List files and folders in fldr -----
function ListOfFilesInAFolder(fldr) 
{
 fso = new ActiveXObject("Scripting.FileSystemObject");
 folder = fso.GetFolder(fldr);
 fc = new Enumerator(folder.subfolders);
 allfiles="";
document.write("<form name='frm1'>")
document.write("<select name='mySelect' size=15>")
for(;!fc.atEnd();fc.moveNext()) 
 {
	doc=fc.item()
	document.write("<option value='"+doc+"'>"+doc+"</option>")
 }
document.write("</select>")
document.write("<p><input type='button' value='show selected..' onClick='ListDocs(this.form);'</p>")
document.write("</form>")
}
 
 
 
<!---- Function Listdocs - List Files and Folders in made selection formObj  ----
function ListDocs(formObj){
output = "";
 
for(var i = 0; i < formObj.mySelect.options.length; i++){
   if(formObj.mySelect.options[i].selected){
     output += ", " + formObj.mySelect.options[i].value;
   }
 }
output = output.substring(2); 
fso = new ActiveXObject("Scripting.FileSystemObject");
folder = fso.GetFolder(output);
fc = new Enumerator(folder.subfolders);
 
document.write("<form name='frm1'>")
document.write("<select name='mySelect' size=15>")
for(;!fc.atEnd();fc.moveNext()) 
 {
	doc=fc.item()
	document.write("<option value='"+doc+"'>"+doc+"</option>")
 }
document.write("</select>")
document.write("<p><input type='button' value='show Select..' onClick='dply();' </p>")
document.write("</form>")
 
}
 
 
<!--- Function to test the onclick event in funtion ListDocs ---- 
function dply(){
alert("Test");
}
 
ListOfFilesInAFolder("C:\\");
</script>
 
</HTML>

Open in new window

0
Comment
Question by:tigger40ks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 14

Accepted Solution

by:
B_Dorsey earned 500 total points
ID: 20371657
I rewrote your code cause it had extra code, Im sure you can get what you want out of what I have supplied.

b
<HTML>
<HEAD>
<TITLE>"List of Folders"</TITLE>
</HEAD>
 
<script LANGUAGE="JavaScript">
doc = "";
function ListOfFilesInAFolder(fldr){
 var sForm = document.frm1.mySelect
 fso = new ActiveXObject("Scripting.FileSystemObject");
 folder = fso.GetFolder(fldr);
 fc = new Enumerator(folder.subfolders);
 allfiles="";
 sForm.options.length = 0; 
 for(;!fc.atEnd();fc.moveNext()){
  doc=fc.item()
  myOption = document.createElement('Option');
  myOption.text = doc;
  myOption.value = doc;
  sForm.add(myOption);
 }
}
 
</script>
</head>
<body onLoad="ListOfFilesInAFolder('C:\\');">
<form name="frm1">
<select name="mySelect" size="15"></select>
<p><input type="button" value="show selected.." onClick="ListOfFilesInAFolder(this.form.mySelect.value);"></p>
</form>
</body>
</HTML>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20374064
And the reason for the error is that if you document.write after the page has rendered (e.g. not inline) the page and the scripts on the page are wiped!
0
 

Author Closing Comment

by:tigger40ks
ID: 31411587
Thank you. Now all I need to do is add coding to decide if I select a file or folder. I have learned more on dynamic scripting.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question