Link to home
Start Free TrialLog in
Avatar of matthewblest2
matthewblest2

asked on

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

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
Avatar of bobbit31
bobbit31
Flag of United States of America image

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

if (f.exists() && f.isDirectory) {
   File[] files = f.listFiles();
   System.out.println(files.length);
}
do you want it to count files in subfolders as well?
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);
}



Avatar of kennethxu
kennethxu

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>" );
}
sorry for the similarity, bobit. typed there and forgot to hit the submit until back from lunch.
;) no prob, happens all the time
Avatar of matthewblest2

ASKER

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
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
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
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
just put it into

<%
...
%>
matthewblest2, quite hitting refresh and reload, this will cause your comment to be reposted :)

yes, put them into <% %>

<%@ 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>");
               }

            }
      }
     

%>

ASKER CERTIFIED SOLUTION
Avatar of kennethxu
kennethxu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
File f = new File("path to a directory");
String fname[] = f.list();
int no_files = fname.length;
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>

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
my pleasure to help :)