aspose file tools*
The moose likes Java in General and the fly likes Scanning a whole directory? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Scanning a whole directory?" Watch "Scanning a whole directory?" New topic
Author

Scanning a whole directory?

Tim frank
Greenhorn

Joined: Mar 07, 2006
Posts: 25
Hey guys, this is my first time on this forum, but it looks pretty helpfully so I might stick to it and become a regular.
I'm trying to write a virus scanner program in java, but before I do all the technical stuff i need to simply scan a directory and all its sub directories and then return how many files there are.
So far I've done this

File file = new File(file)
String[] thearray = file.list();

This tells me the contents of the CURRENT directory but not of the subdirectories?
Can anyone help me with this problem of automatically scanning one directory and then all its sub-directories?

Much appriciated
Tim
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Probably the easiest way would be through a recursive call.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

Originally posted by Keith Lynn:
Probably the easiest way would be through a recursive call.


Agreed. And the listFiles() and isDirectory() would be useful too.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tim frank
Greenhorn

Joined: Mar 07, 2006
Posts: 25
How can I use recursion to do it?
Basically I want to scan a folder which will contain any number of sub directories. If I use isDirectory() it simply tells me if its a directory or not. What I need to achieve is to count all the files in the current directory, then scan all the files the first subdirectory and then its subdirectories etc. Then go back and Scan do the same thing with the next subdirectory found in the parent directory.

any suggestions? it would be much appriciated. Im lost
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
This is an example that will simply count how many files are in a directory and subdirectories. You should be able to modify this code to solve the problem.

Tim frank
Greenhorn

Joined: Mar 07, 2006
Posts: 25
Thanks
Tim frank
Greenhorn

Joined: Mar 07, 2006
Posts: 25
Right I've done that code and it produces a "null pointer exception". Im guessing this is because it runs out of files to scan and then tries to scan nothing. So i decided to include a while statment to do the code only while the directory is not null. It looks like this



However, now the program doesn't end... i've tried several combinations such as directory.list() != null, and file.list()!= null with no success. Help would be much appriciated
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Try it like this.

Tim frank
Greenhorn

Joined: Mar 07, 2006
Posts: 25
You're a ledgend. works a charm Thanks
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

In all versions the last line contains some redundancy:
Originally posted by Keith Lynn:

This should be enough code to read:


http://home.arcor.de/hirnstrom/bewerbung
 
 
subject: Scanning a whole directory?