Solved

Find files with JavaScript - Code Modification Required

Posted on 2014-07-22
10
240 Views
Last Modified: 2014-08-09
Guys, I have found some javascript code on the web that i need to modify to complete a task I have been assigned at work.  The code can be found here: http://www.codeproject.com/Articles/2019/Find-files-with-JavaScript and i have attached the file.  

I have a couple of requirements.

The code to run automatically upon the page loading - Is it possible for the code the run automatically when the page loads, so when you open the search.htm page, it simply displays the directory content for the folder hard coded in the webpage?

Hyperlinks to listed files - Could the files listed be hyperlinks to allow the users to click on the files and they open.

Multiple folder outputs - I would need to list multiple folders, so could the code be coded so that I could, say have a

Var1:folder1 path
var2: folder2 path,
etc.

Can someone please help as i have no idea how to modify the code, or if it is even possible.
****************
Search.htm
0
Comment
Question by:creativepresence
  • 6
  • 4
10 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40211830
Before anything else you do realize this won't work in IE11 (and probably IE10 - cannot remember) without the user manually enabling ActiveX and giving it permission.
Is this in a private intranet or for the internet?
0
 

Author Comment

by:creativepresence
ID: 40211838
Thats fine, its for a private internet with just me and a handful of service desk colleagues using the page and have already identified the settings in IE to change. Just need to figure out the code.
0
 

Author Comment

by:creativepresence
ID: 40211841
should read "Intranet"
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 58

Accepted Solution

by:
Gary earned 300 total points
ID: 40211868
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<style>
.Field {color: black; border: 1px solid #FFFFFF; background-color: #FFCC00;}
.Find  {color:blue; font: 10px Arial;}
</style>
<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>" + '<a href="file:///'+FSo.item().name+'">'+FSo.item().name + "</a></font></td><td width=25%><font class=find>" + FSo.item().type + "</font></td><td width=50%><font class=find>"+ String(FSo.item().size/(1024*1024)).slice(0,3) +" MB</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" onload="Scan()">

<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">Named :
    </font></b> </td>
	<td dir="ltr" bgcolor="#FFCC00">
    <input size=50 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">Path : </font></b> </td>
	<td bgcolor="#FFCC00">
    <input size=50 type=text value="C:\" id=path name=path class="Field" ></td>
	</tr><tr>
	<td bgcolor="#FFCC00">&nbsp;</td>
	<td bgcolor="#FFCC00">
    <input type=button value="        Scan          " onclick=Scan() class="Field"></td>
	</tr><tr>
	<td colspan=2 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>

Open in new window

0
 

Author Comment

by:creativepresence
ID: 40213772
Hi Gary, thats great and thanks very much.  Is there are way to use the code multiple times within the page?  with the least overhead and also the input boxes not to be visible. So ultimately, the user loads the page and is displayed with a list of files from various folders.

New start processes
List of docs
{list file path in the var, that then calls the code and displays the list of docs in the folder in the var}

Leavers Process
Another List of doc
{list another file path in the var, that then calls the code and displays another list of docs in the folder in the var}

Transfer Processes
Another List of docs.
{another list file path in the var, that then calls the code and displays another list of docs in the folder in the var}
0
 

Author Comment

by:creativepresence
ID: 40214061
Hi, slowly getting there, i have it displaying the docs, last modified date & size, plus none of the form details, but ideally as above, need to be able to display multiple folders, plus if there are folders within the displayed folder, to also display the folders.  Also, ideally, exclude i guess hidden or atleast the thumbs.db file. Oh, and ideally file links to open in new window.  

Source:  \\Server\path\path\path\

New Starter Setup Procedure.doc                Fri May 16 14:11:40 UTC+0100 2014            2.6 MB
Thumbs.db    (needs to be hidden.              Tue May 15 15:34:59 UTC+0100 2012          0.0 MB
New Starter Setup Checklist.doc                  Tue Sep 24 12:45:46 UTC+0100 2013           2.4 MB
Folder1 (need to be able to see any folders and ideally linked).

I will endevour to try and figure it out, but any help would be appreciated.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40214374
Have a read thru the 4guysfromrolla article
http://www.4guysfromrolla.com/webtech/faq/FileSystemObject/faq5.shtml

Don't know if I have time today to do anything.

To open in a new window add target="_blank" to the anchor tag
0
 
LVL 58

Expert Comment

by:Gary
ID: 40214576
Ignore the link about its the wrong type

To hide hidden files

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){
			if (FSo.item().Attributes & 2 ){}
			else{
				StrOut += "<tr "+ ((i%2)? "":"bgcolor=#DDAA55")  +"><td width=50%><font class=find>" + '<a href="file:///'+FSo.item().name+'">'+FSo.item().name + "</a></font></td><td width=25%><font class=find>" + FSo.item().type + "</font></td><td width=50%><font class=find>"+ String(FSo.item().size/(1024*1024)).slice(0,3) +" MB</font></td></tr>";
				i++
				}
			}
	}
}

Open in new window

0
 

Assisted Solution

by:creativepresence
creativepresence earned 0 total points
ID: 40219195
Hi Gary/Plus Others, from my reading it would seem that the "outPut.innerHTML" command isnt fit for my purpose as the content is over written with the new content everytime its loaded.  Are they any other alternative commands that will work with the above code to display multiple folder contents? It would be really cool if I could get this sorted as it would get me some serious brownie points at work.
0
 

Author Closing Comment

by:creativepresence
ID: 40250398
Thanks for your assistance, although my issue is still not fully resolved.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBScript on Html 15 47
Troubles viewing HTML in Outlook 2010 client 3 30
Output to HTML in powershell 3 26
Powershell script does not exit 4 27
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question