Walk a directory/Recursively: Difference between revisions
Rename Perl 6 -> Raku, alphabetize, minor clean-up
No edit summary |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 1,329:
/Users/xxx/music/albumx/trackx2.mp3
/Users/xxx/music/albumy/tracky.mp3 ...</pre>
=={{header|Lua}}==
Lua provides functions such as os.execute([command]) and io.popen(prog [, mode]). Below an example for Windows users having io.popen at their disposal. Mind you, it may pop-up a command window.
Line 1,600 ⟶ 1,601:
find_files('.', '*.mp3');</lang>
=={{header|Perl 6}}==▼
Using the [https://github.com/tadzik/File-Find/ File::Find] module:▼
<lang perl6>use File::Find;▼
.say for find dir => '.', name => /'.txt' $/;</lang>▼
Alternatively, a custom solution that provides the same interface as the built-in (non-recursive) <tt>dir</tt> function, and uses <tt>gather</tt>/<tt>take</tt> to return a lazy sequence:▼
<lang perl6>sub find-files ($dir, Mu :$test) {▼
gather for dir $dir -> $path {▼
if $path.basename ~~ $test { take $path }▼
if $path.d { .take for find-files $path, :$test };▼
}▼
}▼
.put for find-files '.', test => /'.txt' $/;</lang>▼
Or if you value performance over portability, here's a function that runs the GNU <tt>find</tt> program and returns a lazy sequence of the files it finds. Parameters are not subjected to shell expansion, and the null-byte (which cannot be present in file paths) is used as the path delimiter, so it should be pretty safe.▼
<lang perl6>sub find-files ($dir, :$pattern) {▼
run('find', $dir, '-iname', $pattern, '-print0', :out, :nl«\0»).out.lines;▼
}▼
.say for find-files '.', pattern => '*.txt';</lang>▼
=={{header|Phix}}==
Line 1,872 ⟶ 1,847:
... *.rkt files including in nested directories ...
</lang>
(formerly Perl 6)
▲Using the [https://github.com/tadzik/File-Find/ File::Find] module:
▲<lang perl6>use File::Find;
▲.say for find dir => '.', name => /'.txt' $/;</lang>
▲Alternatively, a custom solution that provides the same interface as the built-in (non-recursive) <tt>dir</tt> function, and uses <tt>gather</tt>/<tt>take</tt> to return a lazy sequence:
▲<lang perl6>sub find-files ($dir, Mu :$test) {
▲ gather for dir $dir -> $path {
▲ if $path.basename ~~ $test { take $path }
▲ if $path.d { .take for find-files $path, :$test };
▲ }
▲}
▲.put for find-files '.', test => /'.txt' $/;</lang>
▲Or if you value performance over portability, here's a function that runs the GNU <tt>find</tt> program and returns a lazy sequence of the files it finds. Parameters are not subjected to shell expansion, and the null-byte (which cannot be present in file paths) is used as the path delimiter, so it should be pretty safe.
▲<lang perl6>sub find-files ($dir, :$pattern) {
▲ run('find', $dir, '-iname', $pattern, '-print0', :out, :nl«\0»).out.lines;
▲}
▲.say for find-files '.', pattern => '*.txt';</lang>
=={{header|Rascal}}==
|