Click here to Skip to main content
Licence CPOL
First Posted 12 Dec 2007
Views 13,908
Downloads 197
Bookmarked 14 times

File Search using Javascript

By | 12 Dec 2007 | Article
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)

About the Author

Arun Jacob

Software Developer
Tata Consultancy Services
India India

Member

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



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 13 Dec 2007
Article Copyright 2007 by Arun Jacob
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid