Click here to Skip to main content
Click here to Skip to main content
Go to top

File Search using Javascript

, 12 Dec 2007
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Arun Jacob
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 PinmemberMember 98220279-Feb-13 12:34 
AnswerRe: Works great but on local server only PinmemberRene Pilon9-Feb-13 13:05 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 13 Dec 2007
Article Copyright 2007 by Arun Jacob
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid