?
Solved

Linking to files using Javascript's FileSystemObject and Enumerator

Posted on 2003-10-27
11
Medium Priority
?
471 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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

571 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