Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Code to list files in a certain directory

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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,925
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    This cannot be done with client-side JavaScript.

  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    80
    Thanks
    1
    Thanked 0 Times in 0 Posts
    If its to be done on your local machine use this:

    First run this:
    Code:
     
    <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:
    Code:
     
    <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
    Last edited by LFCFan; 05-11-2007 at 12:02 PM.
    http://www.Kanotix.com --My OS of choice, the best hardware detection on the planet!-
    http://distrowatch.com -Linux is the future-
    http://www.packetstormsecurity.org -Security Gurus-

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,633
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Can this be done without writing to a file in C:\....?

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,633
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,633
    Thanks
    0
    Thanked 649 Times in 639 Posts
    sShell.Run("%comspec% /k dir c:\> c:\myTextFile.txt")
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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....

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Escape the \ by making it \\

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

  • #11
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hi glenngv...
    i'm from philippines too.... mabuhay!

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

    automation server cant create object

  • #12
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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.

  • #13
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi glenngv...

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

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

  • #14
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks... glen for suggesting the use of hta...indeed it worked!

  • #15
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    in addition...something was lacking...and after adding the variable it worked!

    Code:
    <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>


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •