?
Solved

Won't count files in a directory

Posted on 2003-03-04
6
Medium Priority
?
163 Views
Last Modified: 2010-04-01
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?

0
Comment
Question by:tigerjade_nc
[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
  • 3
  • 2
6 Comments
 
LVL 14

Accepted Solution

by:
kennethxu earned 300 total points
ID: 8068843
>> String directory = courseId + "\\" + chapter;
>> File chapPages = new File(directory);
your directory is not pointing to the right place

where is your directory located, if it is localted at yourwebapproot/courseId/chapter, try:

String directory = application.getRealPath( "/" + courseId + "/" + chapter );
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8070191
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();
0
 

Author Comment

by:tigerjade_nc
ID: 8071896
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.  
0
Independent Software Vendors: 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!

 

Author Comment

by:tigerjade_nc
ID: 8072044
I found a weird work-around.  I moved the files to another level, and now it's counting!  Thanks, y'all!
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8073641
>> 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 :)
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8073644
>> I swear that your path is pointing to the right place
i mean "NOT pointing", sorry :)
0

Featured Post

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!

Question has a verified solution.

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

Learn how to use the free Acronis True Image app to easily transfer data between iPhones and Android phones.
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 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