This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Scanning a whole directory?  RSS feed

 
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: 23832
140
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: 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
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:
 
Water proof donuts! Eat them while reading this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!