How to recursively in perl readdir contents starting from root and then according to a user specified level retrieve files that end in .txt

StackOverflow https://stackoverflow.com/questions/22138447

Question

I was trying to ask for help I posted a previous question. Also I don't want to use any modules unless it is a built in module I prefer to write my own. I know the recursive part to list all files from multiple directories, but don't understand where exactly or how I would specify the desired level of search, so if I give as parameters root and 3 it should look through at least 3 directories and then retrieve all files as long it is less than or equal to 3. Any help is greatly appreciated.

Était-ce utile?

La solution

do you just want it to list all files, or to return them in an array. If merely printing them is enough, you do something like:

sub print_txt_recurse() {
    my ($filepath, $level) = @_;
    #some code to get file paths and and print txt files going through each file
    elsif (-d $file && $level > 1 ) {
        print_txt_recurse($file, $level - 1);
    }
    return;
}

Autres conseils

You could use File::Find, a core module of Perl, which means it supposes to be available everywhere.

See Core modules (F)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top