Solved

Find files with JavaScript - Code Modification Required

Posted on 2014-07-22
10
221 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Summernote and form validation 10 40
Help with HTML 7 38
Video on my site 4 26
Bootstrap Columns not Side-by-Side 3 11
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now