?
Solved

Linking to files using Javascript's FileSystemObject and Enumerator

Posted on 2003-10-27
11
Medium Priority
?
459 Views
Last Modified: 2009-12-16
First of all i'm a noob when it comes to Javascript and all the knowledge I have I took from the internet and other users' scripts so please bare with me on this one.

I have the following issue. I want to produce a CD with (among others) a HTML file with the capability of reading files on that CD and linking to those files in the HTML file. Now my problem is that the files on the CD's never have the same filename.

Basically what i'm trying to do is the following:

1) CD autoruns the HTML file (no problems with this!)
2) HTML file has a script that looks at the different files on the CD and returns them as clickable links in the HTML

The code I have so far is:

<script language ="Javascript">

function ShowFolderFileList()
{
   alert("Please click yes to the following system message in order for this link to work properly")
   var drive_letter, fso, f, fc, s, sFile, text, text2;
   text=document.location.pathname
   drive_letter=(text.slice(1,2))
   fso = new ActiveXObject("Scripting.FileSystemObject");
   f = fso.GetFolder(drive_letter+":\\");
   fc = new Enumerator(f.files);
   s = "";
   var counter = 0
   for (; !fc.atEnd(); fc.moveNext())

   {
      s += fc.item();
      s += "<BR>"
      counter++;
   }
   
   //text2 = (s.slice(3,100))
   // document.location.href=("file:///"+drive_letter+":/"+text2)
   // alert("You got " + counter + " files in that folder!");
   return(s)

}
</script>


Obviously this is only working partially. I'm now able to display all files in the core directory of the CD, but I can't seem to figure out how to pick up one filename and turn that into a link. With "Return(s)" I get all filenames. How can I turn those filenames to a string so I can use them in a link? All your help is much appreciated!!

Regards,

Steven
0
Comment
Question by:sgoudart
[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
  • 7
  • 4
11 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9626962
Change
 s += fc.item();

to
 s += '<a href="'+fc.item()+'">'+fc.item()+'</a>';
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9626967
and do
document.write(ShowFolderFileList())
0
 

Author Comment

by:sgoudart
ID: 9627125
Thanks, that worked great. The files are now displayed as links. However I want to include those links into the HTML file's text the moment the file is loaded (probably with an 'onload' command??). It should look something link this:

<html>
<head>
<script>
......javascript.....
</script>
<body>
....text....

<a href=javascript:"[file number 1]">Click here to open file number 1</a>
</body>
</html>

Is that at all possible?

0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9627191
Nope - just

<body>
....text....
<script>
document.write(ShowFolderFileList())
</script>
</body>


where you want the list to appear
0
 

Author Comment

by:sgoudart
ID: 9627401
Oh well that's too bad. Is it then possible to filter out certain files, for instance only show files where the extention is PDF or CSV?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9627474
You misunderstand.

No reason to use onLoad
Just put the script

<script>
document.write(ShowFolderFileList())
</script>


whereever you want the list to appear.

Yes you can filter:
change to

  for (; !fc.atEnd(); fc.moveNext()) {
    it = fc.item();
    if (".pdf.csv".indexOf(it.toLowerCase().substring(it.lastIndexOf('.')) !=-1)
     s += '<a href="'+it+'">'+it+'</a><br>';
     counter++;
   }
   
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9627482
or

for (; !fc.atEnd(); fc.moveNext()) {
    it = fc.item();
    if (".pdf.csv".indexOf(it.toLowerCase().substring(it.lastIndexOf('.')) !=-1) {
     s += '<a href="'+it+'">'+it+'</a><br>';
     counter++;
   }
}

if you only want to count the pdf and cvs
0
 

Author Comment

by:sgoudart
ID: 9627604
Thanks for the help. I really appreciate it. However I must be doing something wrong with the filter, because now it's not showing any files at all when calling document.write. I made sure the directory has pdf and csv file in it, so that shouldn't be the problem. The code I have now is:

function ShowFolderFileList()
{
   // alert("Please click yes to the following system message in order for this link to work properly")
   var drive_letter, fso, f, fc, s, sFile, text, text2;
   text=document.location.pathname
   drive_letter=(text.slice(1,2))
   fso = new ActiveXObject("Scripting.FileSystemObject");
   f = fso.GetFolder(drive_letter+":\\");
   fc = new Enumerator(f.files);
   s = "";
   for (; !fc.atEnd(); fc.moveNext())

     {
     it = fc.item();
     if (".pdf.csv".indexOf(it.toLowerCase().substring(it.lastIndexOf('.')) !=-1)
          {
          s += '<a href="'+it+'">'+it+'</a><br>';
          }
     }


return(s)
}


and


<script>
document.write(ShowFolderFileList())
</script>


What am I doing wrong here?

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9627823
Maybe I have a typo.. I will look
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 9627914
There was a missing ) and item is not a string

function ShowFolderFileList() {
  // alert("Please click yes to the following system message in order for this link to work properly")
  var drive_letter, fso, f, fc, s, sFile, text, text2;
  text=document.location.pathname
  drive_letter=(text.slice(1,2))
  fso = new ActiveXObject("Scripting.FileSystemObject");
  f = fso.GetFolder(drive_letter+":\\");
  fc = new Enumerator(f.files);
  s = "";
  for (; !fc.atEnd(); fc.moveNext()) {
    it = ""+fc.item();
    extPos = it.lastIndexOf(".");
    if (extPos > 0 && 
      ".pdf.csv".indexOf(it.toLowerCase().substring(extPos)) !=-1) {
       s += '<a href="'+it+'">'+it+'</a><br>';
    }
  }
  return(s)
}
</script>




<script>
document.write(ShowFolderFileList())
</script>

0
 

Author Comment

by:sgoudart
ID: 9628326
Thanks a lot for your help!
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

762 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