// takes an array of file names or directory names on the command line.
// determines what kind of name it is and processes it appropriately
using system;
using system.io;
using system.collections;
public class recursivefileprocessor {
public static void main(string[] args) {
foreach(string path in args) {
if(file.exists(path)) {
// this path is a file
processfile(path);
}
else if(directory.exists(path)) {
// this path is a directory
processdirectory(path);
}
else {
console.writeline("{0} is not a valid file or directory.", path);
}
}
}
// process all files in the directory passed in, and recurse on any directories
// that are found to process the files they contain
public static void processdirectory(string targetdirectory) {
// process the list of files found in the directory
string [] fileentries = directory.getfiles(targetdirectory);
foreach(string filename in fileentries)
processfile(filename);
// recurse into subdirectories of this directory
string [] subdirectoryentries = directory.getdirectories(targetdirectory);
foreach(string subdirectory in subdirectoryentries)
processdirectory(subdirectory);
}
// real logic for processing found files would go here.
public static void processfile(string path) {
console.writeline("processed file {0}.", path);
}
}
