Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read/print a list of files within a directory 1

Status
Not open for further replies.

jasonsalas

IS-IT--Management
Jun 20, 2001
480
GU
Hi everyone,

I'm trying to see if this would work in JavaScript...I'd like to read the entire contents of a directory and then print out the contents as XML, or optimally, generate an XML file and write it to disk. The server upon which I'm going to use this script doesn't have any ral application platform at all (PHP, ASP, ASP.NET, JSP), so this is pretty raw.

I'm admittedly not the best expert when it comes to JavaScript's I/O capabilities/limitations.

Thanks!
 
Do you mean you want to do this as a Windows Scripting Host file, or in an HTML page?

Lee
 
You'd be using the FileSystemObject either way. You can do a search for that term, though right now the latest answers to questions are in limbo with the search functions for the site.

Lee
 
One more thing...is there a way to access all the files, and possibly filtem them by extension (MP3), into an array?

I've got this so far:

<script language="JavaScript">

<!--
var fso = new ActiveXObject("Scripting.FileSystemObject");
fileObj = fso.GetFile("C:\\Documents and Settings\\All Users\\Documents\\Desktop\\htlp.html");
document.write(fileObj.Name + ' - ' + fileObj.Size);
-->
</script>
 
check that....i pasted the wrong code. i'm using the following, but for every file it prints out 'undefined':

<script language="JavaScript">

<!--
var fso = new ActiveXObject("Scripting.FileSystemObject");
dir = fso.GetFolder("C:\\Documents and Settings\\All Users\\Documents\\Desktop");
f = dir.Files;

for(var i=0;i<f.Count;i++)
{
document.write(f.Name + '<br/>');
}
-->
</script>
 
jasonsalas,

Do this instead.
[tt]
var f=new Enumerator(dir.Files);
for (;!f.atEnd();moveNext()) {
document.write(f.Name+'<br />');
}
[/tt]
regards - tsuji
 
hi tsuji,

thanks for the tip. the code iterates properly through the collection...but unfortunately, i'm still getting 'undefined' for every element;

<script language="JavaScript">
<!--
var fso = new ActiveXObject("Scripting.FileSystemObject");
dir = fso.GetFolder("C:\\Documents and Settings\\Mike Dela Rosa\\My Documents\\");

var f = new Enumerator(dir.Files);

for(;!f.atEnd();f.moveNext()) {
document.write(f.Name + '<br/>');
}
-->
</script>
 
I got it...it's:

document.writeln(f.item().Name);

thanks for your help. i appreciate it!
 
Yes, sure. I did not mentally prepare to modify further downward enough. Glad you get it.

- tsuji
 
just for reference, this is the final script:

<script language="JavaScript">
<!--
var fso = new ActiveXObject("Scripting.FileSystemObject");
dir = fso.GetFolder("C:\\Documents and Settings\\Mike Dela Rosa\\My Documents\\");

var f = new Enumerator(dir.Files);

for(;!f.atEnd();f.moveNext()) {
// filter out only the Word documents
if(f.item().Name.indexOf(".mp3") != -1) {
document.write(f.item().Name + '<br/>');
}
}
-->
</script>
 
just for reference, this is the final script:

<script language="JavaScript">
<!--
var fso = new ActiveXObject("Scripting.FileSystemObject");
dir = fso.GetFolder("C:\\Documents and Settings\\All Users\\My Documents\\");

var f = new Enumerator(dir.Files);

for(;!f.atEnd();f.moveNext()) {
// filter out only the Word documents
if(f.item().Name.indexOf(".mp3") != -1) {
document.write(f.item().Name + '<br/>');
}
}
-->
</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top