...

View Full Version : Code to list files in a certain directory



ken_shoti
05-11-2007, 08:33 AM
Can anyone post a code that will list all filenames in a certain directory...
i know this will be using some sort of activex....i have been searching for such code, but it seems i only found serverside scripts which i dont really need...i need it only for my personal and local usage...

Thanks in advance...

Philip M
05-11-2007, 11:32 AM
This cannot be done with client-side JavaScript.

LFCFan
05-11-2007, 12:57 PM
If its to be done on your local machine use this:

First run this:


<script language = "javascript">
function runShell()
{
var sShell = new ActiveXObject("WScript.Shell")
sShell.Run("%comspec% /k dir yourDirectoryHere > c:\myTextFile.txt)
delete sShell
alert("DIR command complete!")
}
</script>
Replace /k with /c if you don't want to see the command window appear.


Then this:


<script language = "javascript">
function readOut()
{
var fObj = new ActiveXObject("Scripting.FileSystemObject")
var oFile = fObj.OpenTextFile("C:\myTextFile.txt")

while(oFile.AtEndOfStream != true)
{
tVar+=oFile.ReadLine()
}
document.body.innerText = tVar

oFile.Close()
delete fObj
}
</script>



This is very rough and ready, but should work.

Hope that helps
Joe

felgall
05-11-2007, 01:03 PM
Ogf course you will need to be running Windows and Internet Explorer to be able to run that JScript. It can't be done in JavaScript.

ken_shoti
05-12-2007, 03:47 AM
Can this be done without writing to a file in C:\....?

felgall
05-12-2007, 04:02 AM
Just change

sShell.Run("%comspec% /k dir yourDirectoryHere > c:\myTextFile.txt)

to redirect to wherever you want the output to go.

Remove the > c:\myTextFile.txt and it will just write to the command prompt window that it opens.

ken_shoti
05-12-2007, 04:21 AM
hm...the code seems not to work for me...i'm using IE7...
it says on this line:

sShell.Run("%comspec% /k dir c:\> c:\myTextFile.txt)

unterminated string contstant

felgall
05-12-2007, 04:30 AM
sShell.Run("%comspec% /k dir c:\> c:\myTextFile.txt")

ken_shoti
05-12-2007, 05:32 AM
have you tried loading the code?
do i have to load the first function?
i tried placing double quote, but nothing happens..so i load the 1st function....but it complains of a new error...something...server object....

glenngv
05-12-2007, 05:57 AM
Escape the \ by making it \\


sShell.Run("%comspec% /k dir c:\\whateverfolder > c:\\myTextFile.txt");

ken_shoti
05-12-2007, 03:41 PM
hi glenngv...
i'm from philippines too.... mabuhay!

i tried and modified it.....it complains of:

automation server cant create object

glenngv
05-12-2007, 07:19 PM
Hi ken_shoti! Kumusta? From what place are you?

Maybe ActiveX is disabled by default in IE7. Try renaming the file to .hta to make it not enforce the strict security model found in normal HTML pages. More info on HTAs here (http://msdn2.microsoft.com/en-us/library/ms536496.aspx).

ken_shoti
05-13-2007, 04:26 AM
Hi glenngv...

galing po sa Maynila...kayo po?...salamat po sa payo...

Thanks for the advice... i'll try that!

ken_shoti
05-13-2007, 06:15 AM
thanks... glen for suggesting the use of hta...indeed it worked!

ken_shoti
05-13-2007, 06:17 AM
in addition...something was lacking...and after adding the variable it worked!


<script language = "javascript">
function readOut()
{
var tVar
var fObj = new ActiveXObject("Scripting.FileSystemObject")
var oFile = fObj.OpenTextFile("C:\myTextFile.txt")

while(oFile.AtEndOfStream != true)
{
tVar+=oFile.ReadLine()
}
document.body.innerText = tVar

oFile.Close()
delete fObj
}
</script>

glenngv
05-13-2007, 06:21 AM
Walang anuman! Glad to help. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum