• Post Reply Bookmark Topic Watch Topic
  • New Topic

Traversing a File Hierarchy ?  RSS feed

 
Warren Bell
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to traverse a file system and process certain types of files. I have done this before, but I can not find my code or an example. I just need the basic code that allows me to loop thru the contents of a directory. When I find another directory go into it and loop thru it's contents and so on. I do not need any of the File code just the looping code. I think there is a pattern for this, I just don't remember the name.

Thanks,

Warren Bell
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recursion is one way to do this.
Create a method that accepts a File object as a parameter. In the method you check whether the object represents a file or a directory. If it's a directory, get a list of all its child files/directories. Loop thru this list, calling this method for each object.
Once you've written this method, you call it passing a File object that represents your base directory.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!