Solved

Linking to files using Javascript's FileSystemObject and Enumerator

Posted on 2003-10-27
11
436 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now