Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

how can i identify some folders and ignore others?

Following on from my previous question of counting files and folders, i need to isolate particular folder names and how many file are within, such as foldername/number of files has this many items, second folder has this many. thanks

here is the previous code i am working with:

<%@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
matthewblest2
Asked:
matthewblest2
  • 5
  • 2
1 Solution
 
kennethxuCommented:
not sure what exactly you are looking for, but if you want to ignore a folder, say WEB-INF, you can do this by:
change
if( files[ii].isDirectory() ) subCount += listDir( out, files[ii] );
to
if( files[ii].isDirectory() && !files[ii].getName().equals( "WEB-INF" ) ) subCount += listDir( out, files[ii] );

if I didn't get you, please give some example. thanks.
0
 
matthewblest2Author Commented:
what i'd really like is to be able to identify 5 folders, count how many files there are in them and present the results in a table, so in one website there are at least 80 sub webs, each subweb would need to be identified and the five folders within that subweb listed with the number of files in. if i can do this from one jsp page and add up the results of each column at the end of the table that would be great, thanks

Top Root of Website/
website_one:
has four files in folder A
36 in folder b
45 in folder c
12 in folder d
46 in folder e

website_two:
has four files in folder A
26 in folder b
55 in folder c
126 in folder d
6 in folder e
0
 
kennethxuCommented:
String name = files[ii].getName();
if( files[ii].isDirectory() && (name.equals("A") || name.equals("b") || name.equals("c") || name.equals("d") || name.equals("e")) ) subCount += listDir( out, files[ii] );
0
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!

 
kennethxuCommented:
sorry, the above won't work. are you sure that you only have 2 levels?
0
 
kennethxuCommented:
<%@page import="java.io.*, javax.servlet.jsp.JspWriter" %>
<%!
int countFile( JspWriter out, File dir ) throws IOException {
   File[] files = dir.listFiles();
   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 + " files" );
   out.println( ".</ul>" );
   return count;
}

int listSubDir( JspWriter out, File dir ) throws IOException {
   File[] files = dir.listFiles();
   int subCount = 0;
   for( int ii=0, end=files.length; ii < end; ii++ ) {
        String name = files[ii].getName();
        if( files[ii].isDirectory() && (name.equals("A") || name.equals("b") || name.equals("c") || name.equals("d") || name.equals("e")) ) {
             subCount += countFile( out, files[ii] );
        }
   }
   return subCount;
}

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 += listSubDir( out, files[ii] );
     }
   return subCount;
}
%>
<%
   String path = application.getRealPath( "/" );
   File dir = new File( path );
   listDir( out, dir );
%>

0
 
matthewblest2Author Commented:
its a very unsually done web, there is never more than this structure to it, it was restricted in scope to keep things easy for non web people to edit:

top_websitefolder>

topic_webfolder>
folder_one
folder_two
folder_three
etc.

topic_webfolder_two>
folder_one
folder_two
etc.

these 'folders' never have any folders within them, just web pages.
0
 
kennethxuCommented:
ok, then have you tried my code and is there any problem?
0
 
girionisCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to kennethxu

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

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.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now