?
Solved

Count Number of Files in a Folder then display them using JSP

Posted on 2003-02-26
18
Medium Priority
?
1,303 Views
Last Modified: 2008-03-17
using JSP, how can i count the number of webpages within a website folder then display them on a page, i have at least 20 folders and their content to count.

if you have code or a link to something i can use that would be great, cheers
0
Comment
Question by:matthewblest2
  • 6
  • 5
  • 5
  • +2
18 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 8027370
File f = new File ("<path to folder>");

if (f.exists() && f.isDirectory) {
   File[] files = f.listFiles();
   System.out.println(files.length);
}
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8027374
do you want it to count files in subfolders as well?
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8027410
slight modification (will exclude directories in folder):

File f = new File ("<path to folder>");

if (f.exists() && f.isDirectory) {
   File[] files = f.listFiles();
   int numFiles=0;
   for (int i=0;i<files.length;i++) {
      if (files[i].isFile()) {
          numFiles += 1;
          out.println(files[i].getName());
      }
   }
   out.println("File Count: " + numFiles);
}



0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 14

Expert Comment

by:kennethxu
ID: 8027544
String path = application.getRealPath( "/folder/name" );
File dir = new File( path );
File[] files = dir.listFiles();
for( int ii=0, end=files.length; ii < end; ii++ ) {
  if( files[ii].isFile() ) out.println( files[ii].getName() + "<p>" );
}
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8027576
sorry for the similarity, bobit. typed there and forgot to hit the submit until back from lunch.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8027588
;) no prob, happens all the time
0
 

Author Comment

by:matthewblest2
ID: 8027943
thanks for the very quick response, in answer to the subfolders question, yes it will need to look inside all folders and their subfolders too, some files will need to be excluded as well, i really need it to count the number of htm and html files, while ignoring any javascript and cgi i have in the folders.

will give this a go tonight, will post the final code i end up using as well,

Matthew
0
 

Author Comment

by:matthewblest2
ID: 8028029
thanks for the very quick response, in answer to the subfolders question, yes it will need to look inside all folders and their subfolders too, some files will need to be excluded as well, i really need it to count the number of htm and html files, while ignoring any javascript and cgi i have in the folders.

will give this a go tonight, will post the final code i end up using as well,

Matthew
0
 

Author Comment

by:matthewblest2
ID: 8028031
thanks for the very quick response, in answer to the subfolders question, yes it will need to look inside all folders and their subfolders too, some files will need to be excluded as well, i really need it to count the number of htm and html files, while ignoring any javascript and cgi i have in the folders.

will give this a go tonight, will post the final code i end up using as well,

Matthew
0
 

Author Comment

by:matthewblest2
ID: 8028056
how do i add this to a jsp page? (this really shows how much of a newbie i am..)

do you have it as an external file or put it into <% endings?

(don't know why it posted my last comment twice)

matthew
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8028118
just put it into

<%
...
%>
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8028355
matthewblest2, quite hitting refresh and reload, this will cause your comment to be reposted :)

yes, put them into <% %>
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8029380

<%@ page language="java" session="true" contentType="text/html" %>

<%@ page import="java.io.*" %>


<%
      String directory = "/webdata/dev/hp/docs/";   // Change this path your directory path:
      File directoyName = new File(directory);
      out.println ("<b>" + "Directory Listing : " + directory + "</b><br><br>");
     
      String[] fileName = null;

      if (directoyName.isDirectory())  {


            fileName = directoyName.list();
            out.println ("<b>" + "No of Files in folder: " + fileName.length + "</b><br><br>");
            for (int i=0;i<fileName.length;i++) {

               out.println(" File Name : "  + fileName[i]);
               out.println("<br>");
               
               if (new File(directory + fileName[i]).isDirectory()) {
                  directoyName = new File(directory + fileName[i]);
                  out.println("<b>"+ " Directory : "  + fileName[i]+"</b>" );
                  out.println("<br>");
               }

            }
      }
     

%>

0
 
LVL 14

Accepted Solution

by:
kennethxu earned 200 total points
ID: 8029450
i guess matthew mean it recursively, not just one level of subdir. try this code:

<%@page import="java.io.*, javax.servlet.jsp.JspWriter" %>
<%!
int listDir( JspWriter out, File dir ) throws IOException {
     File[] files = dir.listFiles();

     int subCount = 0;
     for( int ii=0, end=files.length; ii < end; ii++ )
          if( files[ii].isDirectory() ) subCount += listDir( out, files[ii] );

     int count = 0;
     out.println( dir.getPath() );
     out.println( "<ul>" );
     for( int ii=0, end=files.length; ii < end; ii++ ) {
          if( files[ii].isFile() ) {
               out.println( "<li>"  + files[ii].getName() + "</li>" );
               count++;
          }
     }
     out.print( "<p>total: " + (count+subCount) + " files" );
     if( subCount > 0 ) out.print(", " + count + " in this dir, " + subCount +" in subdirs" );
     out.println( ".</ul>" );

     return subCount + count;
}
%>

<%
     String path = application.getRealPath( "/" );
     File dir = new File( path );
     listDir( out, dir );
%>
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8032119
File f = new File("path to a directory");
String fname[] = f.list();
int no_files = fname.length;
0
 

Author Comment

by:matthewblest2
ID: 8036687
here is the code that i am now using, i saved this as a .jsp page, one thing to note was copying and pasting (on a mac) carried over some non standard line endings, which meant i was debugging for 20 minutes, thanks to all those that posted a response, much appreciated, matthew

<%@page import="java.io.*, javax.servlet.jsp.JspWriter" %>



<html>



     <head>

          <title>Story</title>

     

          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">





     </head>



     <body bgcolor="#e1fae1" leftmargin="0" marginheight="0" marginwidth="0" topmargin="0">





<%!

int listDir( JspWriter out, File dir ) throws IOException {

File[] files = dir.listFiles();

int subCount = 0;

for(int ii=0, end=files.length; ii < end; ii++ )

if(files[ii].isDirectory() ) subCount += listDir( out, files[ii] );

int count = 0;

out.println( dir.getPath() );

out.println( "<ul>" );

for(int ii=0, end=files.length; ii < end; ii++ ) {

if(files[ii].isFile() )

{

out.println("<li>"+files[ii].getName()+"</li>");

count++;

}

}

out.print( "<p>total: " + (count+subCount) + " files" );

if( subCount > 0 ) out.print(", " + count + " in this dir, " + subCount +" in subdirs" );

out.println( ".</ul>" );

return subCount + count;

}

%>

<%

String path = application.getRealPath("/stories/images/"); // change path name to suit

// String path = application.getRealPath("/stories/");

// String path = application.getRealPath("/");

File dir = new File(path);

listDir(out, dir);

%>



     </body>



</html>

0
 

Author Comment

by:matthewblest2
ID: 8036716
thanks, kennethxu, it got me out of a tight spot at work, and is just what i was after, it was great to see it working for the first time.


cheers, Matthew
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8036986
my pleasure to help :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dropbox has a relatively new feature called Smart Sync.  This feature allows Dropbox Professional (not plus) and Dropbox Business (if enabled) users to store information in Dropbox WITHOUT storing any files on their computer.
As a matter of fact, Outlook OST files are of much importance in relation to Exchange mailbox. OST files are independent as they are simply copy of data of a user’s mailbox on Exchange Server. Though, if the server’s status is changed or it is dama…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question