Click here to Skip to main content
15,867,568 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 493.8K   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

 
QuestionSelect Files Pin
Member 1375190728-Mar-18 9:31
Member 1375190728-Mar-18 9:31 
Questionvery good Pin
Member 895456929-Nov-17 1:21
Member 895456929-Nov-17 1:21 
Answergood work Pin
Member 1331557218-Jul-17 2:41
Member 1331557218-Jul-17 2:41 
Questiongvbfgkfbf Pin
rag12341-May-15 0:52
rag12341-May-15 0:52 
QuestionSubFolders Pin
Member 1104682731-Aug-14 0:16
Member 1104682731-Aug-14 0:16 
QuestionCan not find the file when i'm run pass IIS web server Pin
Member 109795676-Aug-14 15:43
Member 109795676-Aug-14 15:43 
Questionrecurse Pin
Tlhogi2420-May-14 22:24
Tlhogi2420-May-14 22:24 
QuestionERROR .........resultset not positioned properly Pin
Member 965495830-Mar-14 19:41
Member 965495830-Mar-14 19:41 
i am geting error while running my code which is given below. the Error is "Resultset is not positioned properly, perhaps you need to call next"
Please anyone suggest me to resolve it.




@page import="java.util.Calendar"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="javax.swing.JTextArea"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
try
{
ResultSet rs=null,rs1,rs2;
Connection mydbcon=(Connection)session.getAttribute("mydbcon");
Statement statement,stmt;
statement = mydbcon.createStatement();
int length=Integer.parseInt(request.getParameter("lngth"));
String priority=request.getParameter("priority");
int qc_id=Integer.parseInt(request.getParameter("qc_id"));
String team_name=(String)session.getAttribute("team_ldr");
Calendar calendar = Calendar.getInstance();
java.sql.Timestamp jto = new java.sql.Timestamp(calendar.getTime().getTime());
for(int i=0;i<length;i++)
{
String excel=request.getParameter("exl_"+i);
String jbid=request.getParameter("jbid_"+i);
String selct="select * from jobs_detail where excel_name='"+excel+"' and job_id='"+jbid+"'";
rs=statement.executeQuery(selct);
rs.next();
String insrt="insert into jobs_to_qc(excel_name,job_id,production,project_name,priority,qc_id,team_name,assign_time) "
+ "values('"+excel+"','"+jbid+"',"+rs.getInt("production")+",'"+rs.getString("project_name")+"','"+priority+"',"+qc_id+",'"+team_name+"','"+jto+"')";
stmt= mydbcon.createStatement();
stmt.executeUpdate(insrt);
}
%>
<h2 style="color: red">Successfully assigned</h2>
<%
}
catch(Exception ex)
{
ex.printStackTrace();
}

%>
Bugerror Pin
Member 965495816-Feb-14 23:52
Member 965495816-Feb-14 23:52 
GeneralRe: error Pin
Patrik Misencik19-Feb-14 2:24
Patrik Misencik19-Feb-14 2:24 
GeneralRe: error Pin
Member 965495819-Feb-14 18:15
Member 965495819-Feb-14 18:15 
Questionthanks mate Pin
ivan coene17-Jun-13 3:54
ivan coene17-Jun-13 3:54 
GeneralMy vote of 5 Pin
panterall19-Jun-12 4:42
panterall19-Jun-12 4:42 
QuestionHow to add view option Pin
RishiRaut23-Jan-11 7:10
RishiRaut23-Jan-11 7:10 
QuestionHow to upload these files Pin
dsfdfdfdf343434322-Sep-10 3:31
dsfdfdfdf343434322-Sep-10 3:31 
QuestionCan this script be used to search .avi files? Pin
Dave Herrell25-Dec-09 7:09
Dave Herrell25-Dec-09 7:09 
GeneralMy vote of 1 Pin
dvg.madhu7-Oct-09 20:19
dvg.madhu7-Oct-09 20:19 
GeneralUpdate!! - Export to excel spreadsheet added!! Pin
Uno198216-Sep-09 10:14
Uno198216-Sep-09 10:14 
GeneralFIXED!!! New Windows Update Breaks ActiveX security fuctions Pin
Uno198215-Sep-09 10:04
Uno198215-Sep-09 10:04 
GeneralFixed all bugs (toggle and path reset) known issue with script timeout Pin
Uno19822-Sep-09 10:47
Uno19822-Sep-09 10:47 
GeneralRe: Fixed all bugs (toggle and path reset) known issue with script timeout Pin
lunar-fifth14-Sep-09 21:32
lunar-fifth14-Sep-09 21:32 
GeneralRe: Fixed all bugs (toggle and path reset) known issue with script timeout Pin
Uno198215-Sep-09 7:49
Uno198215-Sep-09 7:49 
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 

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.