?
Solved

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

Posted on 2003-02-26
18
Medium Priority
?
1,219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introducing Priority Question, our latest feature.
In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

800 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