Click here to Skip to main content
15,881,840 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 494.9K   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

 
AnswerRe: ActiveXObject("Scripting.FileSystemObject") in Mozilla Pin
Uno198213-Aug-09 9:51
Uno198213-Aug-09 9:51 
GeneralFo is NULL or not an object [modified] Pin
Peter from Hungary21-Jun-06 2:49
Peter from Hungary21-Jun-06 2:49 
GeneralRe: Fo is NULL or not an object Pin
VijayashankarPalanichamy7-Oct-07 21:24
VijayashankarPalanichamy7-Oct-07 21:24 
GeneralRe: Fo is NULL or not an object Pin
Uno19823-Apr-08 10:13
Uno19823-Apr-08 10:13 
GeneralSerach on a server Pin
Carlos S.28-Oct-05 6:27
Carlos S.28-Oct-05 6:27 
AnswerRe: Serach on a server Pin
AFShin Dehghani28-Oct-05 19:50
AFShin Dehghani28-Oct-05 19:50 
QuestionWhat happened??? Pin
Weishun20-Nov-04 15:06
Weishun20-Nov-04 15:06 
AnswerRe: What happened??? Pin
harikagupta4-Oct-07 20:13
harikagupta4-Oct-07 20:13 
GeneralDocumentation Pin
jajansse12-Aug-04 23:05
jajansse12-Aug-04 23:05 
GeneralRe: Documentation Pin
oregano20-Jul-05 17:54
oregano20-Jul-05 17:54 
GeneralRunning this code in a client Pin
Severedlimb24-Jun-03 8:37
Severedlimb24-Jun-03 8:37 
GeneralRe: Running this code in a client Pin
Anonymous20-Jul-05 8:52
Anonymous20-Jul-05 8:52 
GeneralInclude Sub Directory Pin
AFShin Dehghani26-Mar-02 5:50
AFShin Dehghani26-Mar-02 5:50 
GeneralRe: Include Sub Directory Pin
francisco cespedes6-Mar-03 9:48
francisco cespedes6-Mar-03 9:48 
GeneralRe: Include Sub Directory Pin
oregano20-Jul-05 18:04
oregano20-Jul-05 18:04 
GeneralRe: Include Sub Directory Pin
248912831-Aug-06 20:40
248912831-Aug-06 20:40 
GeneralRe: Include Sub Directory Pin
meghak6-Apr-07 17:16
meghak6-Apr-07 17:16 
GeneralRe: Include Sub Directory Pin
bitlisz4-Sep-07 5:36
bitlisz4-Sep-07 5:36 
GeneralRe: Include Sub Directory Pin
harikagupta3-Oct-07 0:43
harikagupta3-Oct-07 0:43 
GeneralRe: Include Sub Directory Pin
Arun Jacob16-Dec-07 18:15
Arun Jacob16-Dec-07 18:15 
GeneralRe: Include Sub Directory Pin
Uno198214-Mar-08 18:33
Uno198214-Mar-08 18:33 
GeneralRe: Include Sub Directory Pin
panterall19-Jun-12 8:53
panterall19-Jun-12 8:53 

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.