#!perl -w
# treewalk.pl - example of walking a directory, for comp.lang.perl.misc

&process_directory (shift @ARGV);

# full version

sub process_directory {
    my ($path) = @_;

    # get the names out of the current directory and separate them into
    # files and subdirectories
    my (@files, @directories);
    my @names = &read_directory ($path);
    for (@names) {
        if (-d "$path/$_") {
            push @directories, $_;
        } else {
            push @files, $_;
        }
    }

    # process all the files
    # for (@files) {
    #     &process_file ("$path/$_");
    # }

    # do any housekeeping here, before recursing into subdirectories
    # M3U CREATION HERE
    if (@directories and
        (@files or
         $#directories > 0)  # don't do dirs with only a single folder within
        ) {
      (my $dospath = $path) =~ s|/|\\|g;
      system(qq|dir /b /s "$dospath\\*.mp3" > "$dospath\\pl_recursive.m3u"|) 
        and die qq|FAILED $!: dir /b /s "$dospath\\*.mp3" > "$dospath\\pl_recursive.m3u"|;
    }

    # process all the subdirectories
    for (@directories) {
        &process_directory ("$path/$_");
    }
}

sub process_file {
    my ($path) = @_;

    # whatever ...

}

# customize the filtering and sorting of names here

sub read_directory {
    my ($path) = @_;

    # get all of the names from a directory, excluding "." and ".."
    local (*DIR);
    opendir (DIR, $path) || die "can't open directory $path: $!";
    my @names = grep (!/^\.\.?$/, readdir DIR);
    closedir DIR;

    # optional - filter out all other names starting with '.'
    @names = grep (!/^\./, @names);

    # optional - sort the names
    # @names = sort @names;

    @names;
}

