Link to home
Start Free TrialLog in
Avatar of tigerjade_nc
tigerjade_nc

asked on

Won't count files in a directory

I'm trying to count the number of files in a directory, to determine navigation.  Here's what I have:

<%@page contentType="text/html" import="java.io.*"%>

<%
//Get the parameters
String courseId          = request.getParameter("courseid");
int chapter          = Integer.parseInt(request.getParameter("chapter"));
int pageIndex          = Integer.parseInt(request.getParameter("page"));
String state          = request.getParameter("state");

//Get the number of pages for the current chapter

String directory = courseId + "\\" + chapter;
int currentChapPages      = 0;
File chapPages = new File(directory);
if (chapPages.isDirectory())  {
     currentChapPages = chapPages.listFiles().length;
}

//Calculate the next and previous chapter & page
int prevChapter          = chapter;
int prevPage          = pageIndex - 1;
int nextChapter          = chapter;
int nextPage          = pageIndex + 1;
boolean showNextButton     = true;
boolean showPrevButton     = true;

//If the current page is the last page, go to the next chapter and first page
if (pageIndex >= currentChapPages) {
     nextChapter++;
     nextPage = 1;

     //Make sure there is a next chapter, if not don't show next button
     String directoryPrev = courseId + "\\" + chapter;
     int prevChapPages      = 0;
     File prevPages = new File(directoryPrev);
     
     if (prevPages.isDirectory())  {
          prevChapPages = prevPages.list().length;
     }
     else {
          showNextButton = false;
     }
}

//If it is the first page, go to the previous chapter except for the How-To (98)
if (pageIndex <= 1 && chapter != 98) {
     prevChapter--;

     //If the the scope is beyond the first chapter, go to the foreword (99)
     if (prevChapter <= 0) {
          prevChapter = 99;
     }

     //Find the last page of the previous chapter if it is not the current chapter
     //Set the prev page to the last page in the previous chapter
     if (prevChapter != chapter) {
          String directoryPrev = courseId + "\\" + chapter;
                int prevChapPages      = 0;
                File prevChapList = new File(directoryPrev);
                if (prevChapList.isDirectory())  {
                    prevChapPages = prevChapList.list().length;
                }
          prevPage = prevChapPages;
     }
}
%>

Seems like it should work, but if I print out the value of currentChapPages or prevChapPages I get zero.  If I declare the variable without assigning it an initial value, it throws a 500 saying "The variable "currentChapPages" may be accessed here before having been definitely assigned a value."  We're running JRun 3.0 on IIS 5.  Anyone have any ideas why I can't get a value out of the currentChapPages bit?

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
Avatar of umangjoshi
umangjoshi

create a File object refer a directory
File fdir = new File("path/url to dir");

the following returns name of files or directory
String files[] = fdir.list();
Avatar of tigerjade_nc

ASKER

The path's correct.  I tried making it full relative as well as absolute, and still didn't get any closer.  The currentChapPages & prevChapPages variables still return zero, no matter what's in the directory.  Neither of these suggestions worked with this bleeping thing.  :(  Anything else I might have missed?  Thanks for the efforts, y'all.  
I found a weird work-around.  I moved the files to another level, and now it's counting!  Thanks, y'all!
>> I found a weird work-around.  I moved the files to another level
I swear that your path is pointing to the right place, and I would never use filesystem relative path, because it is different from server to server. if you must use relative path, use your webapp's relative path with application.getRealPath method as I suggested.

Thanks for your points and good luck :)
>> I swear that your path is pointing to the right place
i mean "NOT pointing", sorry :)