• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scanning a whole directory?

 
Tim frank
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably the easiest way would be through a recursive call.
 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tim frank
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Tim frank
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it like this.

 
Tim frank
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're a ledgend. works a charm Thanks
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In all versions the last line contains some redundancy:
Originally posted by Keith Lynn:

This should be enough code to read:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic