Click here to Skip to main content
15,884,629 members
Articles / Web Development / ASP.NET
Article

File Search using Javascript

Rate me:
Please Sign up or sign in to vote.
3.50/5 (7 votes)
12 Dec 2007CPOL 46.3K   707   14   2
Search files in client machine using javascript.

Introduction

This is used for searching files in the client machine using javascript.

Using the code

Before running this,go to Internet explorer>Tools>Internet options>Security>Customlevel> and then enable 'Initialize and scriptActiveX controls not marked as safe.

I am using an ActiveXObject Scripting.FileSystemObject for searching drives,folders,subfolders and files in the client machine..

var fSObj =new ActiveXObject("Scripting.FileSystemObject");

As the first step on body onload we are filling the dropdownlist ddlDrive with the drives in the system using this ActiveXObject.And after selecting drive and entering file name to search,we are calling a recursive function RecursiveSearch(path) to iterate through all folders and subfolders in the selected drive.
function RecursiveSearch(path)
{
    	var txtSearch=document.getElementById('txtSearch');
	var txtToSearch=new String(txtSearch.value);
	if(txtToSearch.substring(0,2)=="*.")
	{
		txtToSearch=txtToSearch.substring(1,txtToSearch.length);
	}
	txtToSearch=txtToSearch.toLowerCase();
	var enObj = new Enumerator(fSObj.GetFolder(path).Files);
	for(i=0;!enObj.atEnd();enObj.moveNext())
	{
		var fileName=new String(enObj.item(i).name);
		fileName=fileName.toLowerCase();	
		if(txtToSearch.substring(0,1)==".")
			fileName=fileName.substring(fileName.lastIndexOf("."),fileName.length);
		if(txtToSearch==fileName || fileName.search(txtToSearch)>-1)
		{
			..........			}
	}	
	var enObj = new Enumerator(fSObj.GetFolder(path).SubFolders);
	for(i=0;!enObj.atEnd();enObj.moveNext())
	{
		var path=new String(enObj.item(i).path);
		RecursiveSearch(path);
	}	
}

We are also using enumerator while retrieving folders or files.

var enObj = new Enumerator(fSObj.GetFolder(path).Files);
for(i=0;!enObj.atEnd();enObj.moveNext())
{
     ......
}		

Points of Interest

Did you learn anything interesting/fun/annoying while writing the code? Did you do anything particularly clever or wild or zany?

History

Keep a running update of any changes or improvements you've made here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Tata Consultancy Services
India India
I have been working in different .NET Technologies like ASP.NET,WPF,Silverlight for the last few years.I am enjoying my life as a Programmer and spending time with my Family,Friends & Camera.

My Technical Blog


My Photo Blog


Comments and Discussions

 
QuestionWorks great but on local server only Pin
Member 98220279-Feb-13 12:34
Member 98220279-Feb-13 12:34 
AnswerRe: Works great but on local server only Pin
237419-Feb-13 13:05
237419-Feb-13 13:05 
What are the permissions on your pdf folder?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.