JavaScript - Html + JavaScript: get the list of files in current folder on server

Asked By Perry on 27-Oct-10 12:06 PM
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.
anil soni replied to Perry on 27-Oct-10 01:05 PM
Let me you tell that I.E strictly doesnot allow creation on FileSystemObject in HTML but still it is possible to allow activex objects and create an instance of FileSystemOject but it will require many other registry settings. If you are also ready to do the same on each machine whereever the HTML page will be loaded then below is the code for you

<html>
 <head>
  <script language="javascript" >
    
  function getFiles(path)
  {
   var fso = new ActiveXObject("Scripting.FileSystemObject");
   var files = new Array();
   var fso_files = fso.GetFolder(path).Files;
   for(i=0;i<fso_files.length;i++)
   {
  var filePath = fso_files[i].path;
  if(filePath.substring(filePath.length-4,filePath.length).toLowerCase()=="html")
   files.push(fso_files[i].path);
   }
  }
    
  function init()
  {
   var filePaths = getFiles("C:\\htmlsFolder\\");
   var tbTabControl = document.getElementById("tbTabControl");
   var tabPageContainer = document.getElementById("tabPageContainer");
   var tabButtonContainer = document.getElementById("tabButtonContainer");
   tbTabControl.activeTab = null;
   for(var i =0;i<filePaths.length;i++)
   {
  var iFrm = document.createElement("iframe");
  iFrm.src = filePaths[i];
  iFrm.style.display="none";
  iFrm.style.height="600px";
  iFrm.style.width="400px";
  var tabBtn = document.createElement("button");
  tabBtn.style.width="100px";
  tabBtn.innerText="Page";
  tabBtn.tabIFrame=iFrm;
    
  tabBtn.attachEvent("onclick",function()
    {
    if(event.srcElement && event.srcElement.tabIFrame)
    {
      if(tbTabControl.activeTab)
      {
       tbTabControl.activeTab.style.height="20px";
       tbTabControl.activeTab.tabIFrame.style.display="none";
      }
      event.srcElement.style.height = "25px";
      tabIFrame.style.display="block"; 
      tbTabControl.activeTab = event.srcElement;
    }
    } );
  tabButtonContainer.appendChild(tabBtn);
  tabPageContainer.appendChild(iFrm);
  }
  if(tabButtonContainer.childNodes.length>0)
   tabButtonContainer.childNodes[0].click();
  }
  </script>
 </head>
 <body onload="init()">
  <table id="tbTabControl">
   <tr><td id="tabButtonContainer"></td></tr>
   <tr><td id="tabPageContainer"></td></tr>
  </table>
 </body>
</html>


You can use ActiveX Compatibility Manager by nirsoft to enable ActiveX object creation by javascript in IE.
Remember if you will run this script without enabling FileSystemObject ActiveX object you will get following error
Automation server can't create object.
Vasanthakumar D replied to Perry on 27-Oct-10 01:18 PM
Hi,

ActiveX objects executes at client side.. so you cannot get server side folders...

since you are using HTML, you cannot do this without server script like PHP, asp, asp.net etc...

use Ajax to get those details with the help of above server scripts...

Regards,
Vasanth
anil soni replied to Vasanthakumar D on 27-Oct-10 02:00 PM
@Vasanthakumar D
I think he wants to get filelist of local client folder.... if not then its is much there exists a much simpler and neat solution for his requirement
anil soni replied to Vasanthakumar D on 27-Oct-10 02:01 PM
@Vasanthakumar D
I think he wants to get filelist of local client folder.... if not then the same can be implemented with simple and neat approach
undhad ashwin replied to Perry on 27-Oct-10 02:39 PM
<script type="text/javascript">
function showAvailableDrives()
{
document.write( getDriveList() );
}

function getDriveList()
{
var fso, s, n, e, x;
fso = new ActiveXObject('Scripting.FileSystemObject');
e = new Enumerator( fso.Drives );
s = '';
do
{ x = e.item();
s = s + x.DriveLetter;
s += ':- ';
if ( x.DriveType == 3 ) n = x.ShareName;
else if ( x.IsReady )
n = x.VolumeName;
else n = '[Drive not ready]';

s += n + '<br>';
e.moveNext();
}
while ( !e.atEnd() );
return( s );
}
</script>
Perry replied to undhad ashwin on 27-Oct-10 03:45 PM
one more useless answer. You should be depot from this site.
Perry replied to anil soni on 27-Oct-10 03:49 PM
Anil,

Vasanth is correct. The folder is not at local machine. I am using TeamCity where it generates artifacts. I will have html + javascript page along with these artifacts. This script generates the tabbed webpage dynamically for other htmp files available in artifacts.

FYI, Teamcity is build monitoring tool and located on our server. So when you click my web-page it should able to iterate through the folder where it belongs to. That is the files are located on server but web-page running on my local machine.

I am not able to get these list of files.

Thanks.

Perry replied to Vasanthakumar D on 27-Oct-10 03:50 PM
Anil,

Vasanth is correct. The folder is not at local machine. I am using TeamCity where it generates artifacts. I will have html + javascript page along with these artifacts. This script generates the tabbed webpage dynamically for other htmp files available in artifacts.

FYI, Teamcity is build monitoring tool and located on our server. So when you click my web-page it should able to iterate through the folder where it belongs to. That is the files are located on server but web-page running on my local machine.

I am not able to get these list of files.

Thanks.
anil soni replied to Perry on 28-Oct-10 02:54 PM
Please tell me which languange you are using in server side scripting.
Perry replied to anil soni on 28-Oct-10 03:05 PM
You can use any language but it has to be supported in Html page. I am not able to use any other file extension because of Teamcity limitations.

Thanks.
anil soni replied to Perry on 31-Oct-10 07:05 AM
It won't be possible using HTML pages because HTML pages do not get processed at the webserver. HTML pages are only readed and sent to the client by the webserver. There is no server side processing done by the webserver for HTML page hence we cannot perform are server side task of reading all files in and folder and render HTML accordingly in the output when the requested page extension is HTML.
Perry replied to anil soni on 31-Oct-10 11:58 AM
Cool dude. I found the work around my modifying our intermediate system. Thanks for your help on this. Much appreciated!
Charu krish replied to Perry on 22-Mar-12 05:54 AM
hi please give me that work around..even I am facing the same issue.