Walk a directory/Recursively
You are encouraged to solve this task according to the task description, using any language you may know.
Walk a given directory tree and print files matching a given pattern.
Note: Please be careful when running any code examples found here.
IDL
result = file_search( directory, '*.txt', count=cc )
This will descend down the directory/ies in the variable "directory" (which can be an array) returning an array of strings with the names of the files matching "*.txt" and placing the total number of matches into the variable "cc"
Java
Compiler: javac, JDK 1.4 and up
Done using no pattern. But with end string comparison which gave better results.
import java.io.File; public class MainEntry { public static void main(String[] args) { walkin(new File("/home/user")); //Replace this with a suitable directory } /** * Recursive function to descent into the directory tree and find all the file * that end with ".mp3" * @param dir A file object defining the top directory **/ public static void walkin(File dir) { String pattern = ".mp3"; File listFile[] = dir.listFiles(); if(listFile != null) { for(int i=0; i<listFile.length; i++) { if(listFile[i].isDirectory()) { walkin(listFile[i]); } else { if(listFile[i].getName().endsWith(pattern)) { System.out.println(listFile[i].getPath()); } } } } } }
Python
Interpreter: Python 2.5
import fnmatch import os rootPath = '/' # Change to a suitable path for your OS pattern = '*.mp3' # Any string; Can include any UNIX shell-style wildcards # Includes: *, ?, [seq], [!seq] for root, directories, files in os.walk(rootPath): for aFile in files: if fnmatch.fnmatch(aFile, pattern): print os.path.join(root, aFile)
Interpreter: Python 2.5
Libraries: Path
from path import path rootPath = '/' pattern = '*.mp3' d = path(rootPath) for f in d.walkfiles(pattern): print f
Ruby
Pattern matching using regular expressions
#define a recursive function that will traverse the directory tree def printAndDescend(pattern) #we keep track of the directories, to be used in the second, recursive part of this function directories=[] Dir['*'].sort.each do |name| if File.file?(name) and name[pattern] puts(File.expand_path(name)) elsif File.directory?(name) directories << name end end directories.each do |name| #don't descend into . or .. on linux Dir.chdir(name){printAndDescend(pattern)} if !Dir.pwd[File.expand_path(name)] end end #print all ruby files printAndDescend(/.+\.rb$/)
Or use the Find core Module
require 'find' def find_and_print(path, pattern) Find.find(path) do |entry| if File.file?(entry) and entry[pattern] puts entry end end end # print all the ruby files find_and_print(".", /.+\.rb$/)
Scala
This is not implemented in the Scala library. Here is a possible solution, building on class java.io.File and on scala language and library iteration facilities
package io.utils; import java.io.File /** A wrapper around file, allowing iteration either on direct children or on directory tree */ class RichFile(file: File) { def children = new Iterable[File] { def elements = if (file.isDirectory) file.listFiles.elements else Iterator.empty; } def andTree : Iterable[File] = Seq.single(file) ++ children.flatMap(c => c.andTree) // Resuting iterable contains the root item file (as a sequence of a single element : Seq.single(file) // Iterable.flatMap : given and a function that maps each item to an Iterable, // concat all resulting iterables into a single one. } /** * When object RichFile is imported, method toRichFile will be automatically called * around a file instance when needed. If f is a file, code f.children (resp. f.andTree) will be equivalent to * toRichFile(f).children (resp. toRichFile(f).andTree). So it looks as if methods children and andTree had been * added to class File */ object RichFile { implicit def toRichFile(file: File) = new RichFile(file) }
Using it :
package test.io.utils; import RichFile._; // this makes implicit toRichFile active import java.io.File; object Test extends Application { File root = new File("/home/user") for(val f <- root.andTree) Console.println(f) // filtering comes for free for(val f <- root.andTree; f.getName.endsWith(".mp3")) Console.println(f)
}