Click here to Skip to main content
16,017,954 members
Articles / Programming Languages / Javascript

Find Files with JavaScript

Rate me:
Please Sign up or sign in to vote.
4.85/5 (38 votes)
25 Mar 20021 min read 505.3K   13.3K   56   72
Find files or directory in client or server site with JavaScript

Image 1

Introduction

Maybe you want to have list of server or client files in your web page to uploading or downloading or changing. JScript allow you to do this. You can capture all files type, size, properties in all directories and use them easily. In this sample, I use this technique for searching files in local hard drive.

Necessary Attention: Your browser should have access to run ActiveX objects. If receive a error message telling "Automatic Server Can't Create Object", you most go to Internet Option and in security tab, select custom level in Local part and change options to get effect.

ActiveX Objects

For working with Files and directory, you should make a server object as Scripting.FileSystemObject, then with GetDirectory() method can get a directory object. If you want to know whether the directory exists, use FolderExists().

JavaScript
var Fo = new ActiveXObject("Scripting.FileSystemObject");

Enumerator

Enumerators are an array of Directory or File objects such that you can use methods to get special file or sub directory that are in defined path. Enumerators have some methods I have called like atEnd() and moveNext() in my sample.

JavaScript
function FindFile(FOo)
{
    //Get array of files including in FOo directory object
    var FSo = new Enumerator(FOo.Files);
    
    // Getting all files in FSo object
    for (i=0;!FSo.atEnd();FSo.moveNext())

    ...
}

Files Object Properties

For getting a object that is in Enumerator object, you must use item() and it returns a file or directory object that is in current place of Enumerator. Then, you can use Files properties that are listed:

  • Attributes
  • DateCreated
  • DateLastAccessed
  • DateLastModified
  • Drive
  • Name
  • ParentFolder
  • Path
  • ShortName
  • ShortPath
  • Size
  • Type
JavaScript
FSo.item().name // File name
FSo.item().type // Description defining the file extension
FSo.item().size // File size in byte

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.


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThere are 2 bugs left in my source code. Pin
Uno198214-Jul-09 5:43
Uno198214-Jul-09 5:43 
QuestionHI, can i link results to the files!? Pin
ALEX CHIHAI16-Jun-09 10:16
ALEX CHIHAI16-Jun-09 10:16 
AnswerRe: HI, can i link results to the files!? Pin
Uno198214-Jul-09 5:35
Uno198214-Jul-09 5:35 
GeneralRe: HI, can i link results to the files!? Pin
RishiRaut23-Jan-11 7:06
RishiRaut23-Jan-11 7:06 
GeneralAdvanced Search including files from subdirectories, progressbar and delete function Pin
Uno19827-Jan-09 6:38
Uno19827-Jan-09 6:38 
GeneralRe: Advanced Search including files from subdirectories, progressbar and delete function Pin
Uno198222-Apr-09 4:33
Uno198222-Apr-09 4:33 
QuestionCan the results be pushed to an Excel spreadhseet? Pin
popsui14-Oct-08 10:42
popsui14-Oct-08 10:42 
AnswerRe: Can the results be pushed to an Excel spreadhseet? [modified] Pin
Uno198231-Dec-08 9:22
Uno198231-Dec-08 9:22 
I never had this requested but you are right. The option to do such a thing would be very cool. I do know that the output is already being delivered to the screen via tables so getting this into a spreadsheet shouldn't be too hard. I'll work on it in my spare time :P
I've already got the source code modified to where it looks through all folders and no longer displays the .. as a base or any of the folders in the results. I removed the check box to display subfolders because it is no longer needed. The way it works now is you give it a drive or base folder. Example: C:\ it looks through every folder under that root and gives the files located within each folder similar to windows search application. The option to delete any of the files will be to the right and you can still click the file name and view the file. Now the folder path will be displayed in a column to the right of the file (example: if i searched the c:\ then a file might have c:\windows as a path). It will also alert the user and ask are you sure before deleting. I'll have the code posted soon Smile | :)

modified on Wednesday, December 31, 2008 3:34 PM

AnswerRe: Can the results be pushed to an Excel spreadhseet? Pin
Uno198216-Sep-09 10:11
Uno198216-Sep-09 10:11 
GeneralI can see nothing happenned=( Pin
nguk30-May-08 16:35
nguk30-May-08 16:35 
Generalno file found or search in progress [modified] Pin
crichardson1828-Apr-08 7:23
crichardson1828-Apr-08 7:23 
GeneralRe: no file found or search in progress Pin
Uno198213-Aug-09 9:42
Uno198213-Aug-09 9:42 
GeneralRe: no file found or search in progress Pin
crichardson1824-Aug-09 5:57
crichardson1824-Aug-09 5:57 
GeneralRe: no file found or search in progress Pin
Uno198224-Aug-09 8:41
Uno198224-Aug-09 8:41 
GeneralUpdate to sort by date (Delete function added) Pin
Uno19823-Apr-08 6:17
Uno19823-Apr-08 6:17 
GeneralRe: Update to sort by date (Delete function added) [I know this code is buggy] Pin
Uno198231-Dec-08 9:56
Uno198231-Dec-08 9:56 
GeneralVery Cool Pin
jftejadal28-Mar-08 11:36
jftejadal28-Mar-08 11:36 
GeneralUPDATE!! Sorting by Date() function Pin
Uno198227-Mar-08 11:23
Uno198227-Mar-08 11:23 
Questiondo not work in my computer Pin
emari216-Nov-07 4:19
emari216-Nov-07 4:19 
Questionhow to get the textfile from the system without giving the foldername in javascript in dynamically Pin
harikagupta2-Oct-07 20:39
harikagupta2-Oct-07 20:39 
QuestionEnumeration error Pin
SourceKode10-Aug-07 8:30
SourceKode10-Aug-07 8:30 
GeneralGood Very Good Pin
naimisar25-Jul-07 19:00
naimisar25-Jul-07 19:00 
GeneralFinding files without using ActiveX Pin
Keertikiran15-May-07 21:09
Keertikiran15-May-07 21:09 
GeneralRe: Finding files without using ActiveX Pin
Uno198213-Aug-09 9:47
Uno198213-Aug-09 9:47 
Generala comment from nee_bhatia Pin
Sean Ewington8-May-07 3:29
staffSean Ewington8-May-07 3:29 

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.