Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Scanning a whole directory?

 
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
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably the easiest way would be through a recursive call.
 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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: 2410
  • 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: 2410
  • 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
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • 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:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic