Linking to files using Javascript's FileSystemObject and Enumerator

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
sgoudartAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michel PlungjanIT ExpertCommented:
Change
 s += fc.item();

to
 s += '<a href="'+fc.item()+'">'+fc.item()+'</a>';
0
Michel PlungjanIT ExpertCommented:
and do
document.write(ShowFolderFileList())
0
sgoudartAuthor Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Michel PlungjanIT ExpertCommented:
Nope - just

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


where you want the list to appear
0
sgoudartAuthor Commented:
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
Michel PlungjanIT ExpertCommented:
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
Michel PlungjanIT ExpertCommented:
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
sgoudartAuthor Commented:
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
Michel PlungjanIT ExpertCommented:
Maybe I have a typo.. I will look
0
Michel PlungjanIT ExpertCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sgoudartAuthor Commented:
Thanks a lot for your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.