Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

File Search using Javascript

0.00/5 (No votes)
12 Dec 2007 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here