65.9K
CodeProject is changing. Read more.
Home

File Search using Javascript

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.50/5 (7 votes)

Dec 13, 2007

CPOL
viewsIcon

46927

downloadIcon

709

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.