locked
Html + JavaScript: get the list of files in current folder on server RRS feed

  • Question

  • Hi,

    Can you tell me if there is a way to get the list of files in current folder. The current folde is the folder from where my .html file is executing. This will be running at server side probably if it required.

    I have googled everything and got to know there are some ways to do it with FileSystemObject but I am not a big fan of it. I have no problem is giving credentials if required.

    Thanks.
    Wednesday, October 27, 2010 4:06 PM

All replies

  • See this javascript code it might help 

    <SCRIPT LANGUAGE="JavaScript">
    var myloc = window.location.href;
    var locarray = myloc.split("/");
    delete locarray[(locarray.length-1)];
    var arraytext = locarray.join("/");
    </script>
    

    This is doing following simple logic

    if your current html page is in a location like "C:\html\abc\test.html"

    1. myloc = "C:\html\abc\test.html"

    2.locarray ={"c:" , "html", "abc", "test.html"}

    3.in the above locarray (locarray.length-1 is the last element of array )"test.html" will be deleted

    thus what remains is {"c:" , "html", "abc"}

    4. lastly it joins all the elements in the array with "/"

    ie c:/html/abc (which is ur current directory)  :-)

    was this usefull?


    Sri Hari Haran Seenivasan

    Sharing real experience is the best way to make every one Cautious or Happy
    Media Learning will be the future
    Cool Softwares
    As Software Engineers/Professionals we must take care of health (Obesity)
    Thursday, October 28, 2010 1:28 PM
  • Yep. It gives the correct folder name but how would you get the list of files?
    Thursday, October 28, 2010 1:39 PM
  • FYI, I too tried this code (http://javascript.internet.com/navigation/current-directory.html) but it didn't display the available files. Morever, I don't want to display it but I want to have a ForEach loop and want to iterate through the available files.

    Thanks.

    Thursday, October 28, 2010 1:45 PM
  • Help me here. This is challenging. :)
    Thursday, October 28, 2010 2:59 PM
  • Yes , but using FSO will solve the problem

    Take a look at Java Script Extensions then browse the following site.

    http://www.c-point.com/JavaScript/articles/file_access_with_JavaScript.htm

    All the best


    Sri Hari Haran Seenivasan

    Sharing real experience is the best way to make every one Cautious or Happy
    Media Learning will be the future
    Cool Softwares
    As Software Engineers/Professionals we must take care of health (Obesity)
    Thursday, October 28, 2010 3:15 PM
  • Nope...I already said I can't use FSO as the files are stored at server side and anyway nobody prefer to use FSO in JavaScript as no brower support it by default. You have to make lot of registry changes that is not preferable in production environment.

    We now know where is the folder located on server. Can we get the files from my html page itself? I am fine to use any language code inside html page.

    Thanks.

    Thursday, October 28, 2010 3:20 PM
  • Then use an ASPX page and use the sever side script to display the contents.

    ex C# and VB has FileIO objects.


    Sri Hari Haran Seenivasan

    Sharing real experience is the best way to make every one Cautious or Happy
    Media Learning will be the future
    Cool Softwares
    As Software Engineers/Professionals we must take care of health (Obesity)
    Thursday, October 28, 2010 3:34 PM
  • I dont' know ASPX :(
    Thursday, October 28, 2010 3:40 PM
  • Hi I hope this will help:

    <HTML>
    <HEAD>
    <TITLE></TITLE>
    </HEAD>
    <style>
    .Field {color: black; border: 1px solid #FF
    FFFF; background-color: #FFCC00;}
    .Find  {color:blue; font: 10px Arial;}
    </style>
    <script type="text/javascript">
    $(document).ready(function () {
        //Disable cut copy paste
        $('body').bind('cut copy paste', function (e) {
            e.preventDefault();
        });
       
        //Disable mouse right click
        $("body").on("contextmenu",function(e){
            return false;
        });
    });

    </script>
    <script>
    var Fo =new ActiveXObject("Scripting.FileSystemObject");
    var StrOut = new String();
    var FileName = new String();
    var Extention = new String();

    function FindFile(FOo)
    {
        var FSo = new Enumerator(FOo.Files);
        for(i=0;!FSo.atEnd();FSo.moveNext())
        {
            if(FileName == "*" ||  FSo.item().name.slice(0,FSo.item().name.lastIndexOf(".")).toLowerCase().indexOf(FileName)>-1)
                if(Extention == "*" || FSo.item().name.slice(FSo.item().name.lastIndexOf(".")+1).toLowerCase().indexOf(Extention)>-1){
                    StrOut += "<tr "+ ((i%2)? "":"bgcolor=#DDAA55")  +"><td width=50%><font class=find>" + FSo.item().name + "</font></td><td width=50%><font class=find> <a href="+ FSo.item() +">"+ FSo.item().name +"</font></td><td width=25%><font class=find>" + FSo.item().type + "</font></td></tr>";
                    i++
                    }
        }
    }    

    function Scan()
    {
        FileName = (search.value.lastIndexOf(".")>-1)? search.value.slice(0,search.value.lastIndexOf(".")):(search.value.length>0)? search.value.toLowerCase():"*"; //Get Searched File Name
        Extention = (search.value.lastIndexOf(".")>-1)? search.value.slice(search.value.lastIndexOf(".")+1).toLowerCase():"*"; // Get Searched File Extention Name

        if(path.value.length>0 && Fo.FolderExists(path.value)){
            StrOut = "<table border=0 width=100% cellspacing=0>"
            FindFile(Fo.GetFolder(path.value));
            outPut.innerHTML = StrOut+"</table>";
            }
        else alert("Insert Correct Path Address");
    }
    </script>


    <BODY topmargin="0" leftmargin="0">

    <table border=0 width=100% cellspacing="0" style="border-collapse: collapse" cellpadding="2"><tr>
        <td dir="ltr" bgcolor="#FFCC00"><b><font face="Arial" size="2">Enter Name :
        </font></b> </td>
        <td dir="ltr" bgcolor="#FFCC00">
        <input size=60 type=text id=search name=search class="Field"></td>
        </tr><tr>
        <td dir="ltr" bgcolor="#FFCC00">
        <p dir="ltr"><b><font face="Arial" size="2"> </font></b> </td>
        <td bgcolor="#FFCC00">
        <input size=50 type=hidden value="C:\Users\arun\Downloads\Documents" id=path name=path class="Field" ></td>
        </tr><tr>
        <td bgcolor="#FFCC00">&nbsp;</td>
        <td bgcolor="#FFCC00">
        <input type=button value="        Submit          " onclick=Scan() class="Field"></td>
        </tr><tr>
        <td colspan=3 align=right bgcolor="#FFCC00"><font face=arial size=2><b>Search Result</b></font><hr></td>

        </tr><tr>
        <td colspan=2 bgcolor="#FFCC00"><div id=outPut></div></td>
        </tr></table>
    </BODY>
    </HTML>

    Sunday, December 3, 2017 2:55 PM
  • How can I display the content of <g class="gr_ gr_28 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" data-gr-id="28" id="28">arraytext</g>?
    Wednesday, September 26, 2018 6:06 PM