Solved

Linking to files using Javascript's FileSystemObject and Enumerator

Posted on 2003-10-27
11
454 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 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 125 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

724 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