Walk a directory/Recursively: Difference between revisions

Content added Content deleted
m (Moved to FSO cat)
m (Moved "bash" to "UNIX shell", English, Java syntax highlighting, other little fixes)
Line 5: Line 5:


'''Note:''' Please be careful when running any code examples found here.
'''Note:''' Please be careful when running any code examples found here.

=={{header|Bash}}==
<pre>
tree -fi /usr/share/man/ | grep printf

/usr/share/man/fr/man1/printf.1.bz2
/usr/share/man/fr/man3/asprintf.3.bz2
/usr/share/man/fr/man3/dprintf.3.bz2
/usr/share/man/fr/man3/fprintf.3.bz2
/usr/share/man/fr/man3/fwprintf.3.bz2
/usr/share/man/fr/man3/printf.3.bz2
/usr/share/man/fr/man3/snprintf.3.bz2
/usr/share/man/fr/man3/sprintf.3.bz2
/usr/share/man/fr/man3/swprintf.3.bz2
/usr/share/man/fr/man3/vasprintf.3.bz2
/usr/share/man/fr/man3/vdprintf.3.bz2
/usr/share/man/fr/man3/vfprintf.3.bz2
/usr/share/man/fr/man3/vfwprintf.3.bz2
/usr/share/man/fr/man3/vprintf.3.bz2
/usr/share/man/fr/man3/vsnprintf.3.bz2
/usr/share/man/fr/man3/vsprintf.3.bz2
/usr/share/man/fr/man3/vswprintf.3.bz2
/usr/share/man/fr/man3/vwprintf.3.bz2
/usr/share/man/fr/man3/wprintf.3.bz2
/usr/share/man/man1/printf.1.bz2
/usr/share/man/man3/ber_printf.3.bz2
/usr/share/man/man3/curl_mprintf.3.bz2
</pre>


=={{header|D}}==
=={{header|D}}==
module std.file provide different walk directory functions (listdir).<br>
module std.file provides different walk directory functions (listdir).<br>
This one recursively walk the directory, which can either match by regular expression or unix shell style pattern.
This one recursively walks the directory, which can either match by regular expression or unix shell style pattern.
<pre>import std.stdio;
<pre>import std.stdio;
import std.file;
import std.file;
Line 55: Line 27:
writefln(d);
writefln(d);
}</pre>
}</pre>
This one does not itself walk into sub directory, but can be recursive by a callback delegate function.
This one does not itself walk into a sub directory, but can be recursive by a callback delegate function.
<pre>import std.stdio;
<pre>import std.stdio;
import std.file;
import std.file;
Line 158: Line 130:
Done using no pattern. But with end string comparison which gave better results.
Done using no pattern. But with end string comparison which gave better results.


import java.io.File;
<java>import java.io.File;
public class MainEntry {
public class MainEntry {
public static void main(String[] args) {
public static void main(String[] args) {
walkin(new File("/home/user")); //Replace this with a suitable directory
walkin(new File("/home/user")); //Replace this with a suitable directory
}
}
/**
/**
* Recursive function to descent into the directory tree and find all the file
* Recursive function to descend into the directory tree and find all the files
* that end with ".mp3"
* that end with ".mp3"
* @param dir A file object defining the top directory
* @param dir A file object defining the top directory
**/
**/
public static void walkin(File dir) {
public static void walkin(File dir) {
String pattern = ".mp3";
String pattern = ".mp3";
File listFile[] = dir.listFiles();
File listFile[] = dir.listFiles();
if(listFile != null) {
if(listFile != null) {
for(int i=0; i<listFile.length; i++) {
for(int i=0; i<listFile.length; i++) {
if(listFile[i].isDirectory()) {
if(listFile[i].isDirectory()) {
walkin(listFile[i]);
walkin(listFile[i]);
} else {
} else {
if(listFile[i].getName().endsWith(pattern)) {
if(listFile[i].getName().endsWith(pattern)) {
System.out.println(listFile[i].getPath());
System.out.println(listFile[i].getPath());
}
}
}
}
}
}
}
}
}
}
}</java>
}


=={{header|MAXScript}}==
=={{header|MAXScript}}==
Line 253: Line 225:
=={{header|Pop11}}==
=={{header|Pop11}}==


Builtin procedure sys_file_match searches directories or directory
Built-in procedure sys_file_match searches directories or directory
trees using shell-like patterns (three dots indicate search for
trees using shell-like patterns (three dots indicate search for
subdirectory tree).
subdirectory tree).
Line 374: Line 346:
}
}


Class ''RichFile'' gets a ''java.io.File'' in constructor. Its two methods returns ''Iterable''s on items of type File. ''children'' allow iterations on the direct children (empty if file is not a directory). ''andTree'' contains file and all files below, as a concatenation (''++'') of a a sequence which contains only file (''Seq.single'') and actual descendants. Method ''flatMap'' in Iterable takes a function argument which associates each item (''child'') to another Iterable (''andTree'' called recursively on that child) and returns the concatenation of those iterables.
Class ''RichFile'' gets a ''java.io.File'' in constructor. Its two methods return ''Iterable''s on items of type File. ''children'' allow iterations on the direct children (empty if file is not a directory). ''andTree'' contains a file and all files below, as a concatenation (''++'') of a sequence which contains only a file (''Seq.single'') and actual descendants. The method ''flatMap'' in Iterable takes a function argument which associates each item (''child'') to another Iterable (''andTree'' called recursively on that child) and returns the concatenation of those iterables.


The purpose of object ''RichFile'' is to publish implicit method ''toRichFile''. When this method is available in scope (after ''import RichFile.toRichFile'' or ''import RichFile._''), it is called behind the scene when a method of class ''RichFile'' is called on an instance of type File : with f of type File, code ''f.children'' (resp. ''f.andTree'') becomes ''toRichFile(f).children'' (resp. ''toRichFile(f).andTree''). It is as if class File had been added the methods of class RichFile.
The purpose of the object ''RichFile'' is to publish the implicit method ''toRichFile''. When this method is available in scope (after ''import RichFile.toRichFile'' or ''import RichFile._''), it is called behind the scene when a method of class ''RichFile'' is called on an instance of type File : with f of type File, code ''f.children'' (resp. ''f.andTree'') becomes ''toRichFile(f).children'' (resp. ''toRichFile(f).andTree''). It is as if class File had been added the methods of class RichFile.


Using it :
Using it :
Line 412: Line 384:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==


{{platheader|.NET}}{{works with|Visual Basic .NET|9.0+}}
'''Platform:''' [[.NET]]

{{works with|Visual Basic .NET|9.0+}}
This uses the OS pattern matching
This uses the OS pattern matching


Line 425: Line 395:
Next
Next
End Sub
End Sub

=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
<pre>
tree -fi /usr/share/man/ | grep printf

/usr/share/man/fr/man1/printf.1.bz2
/usr/share/man/fr/man3/asprintf.3.bz2
/usr/share/man/fr/man3/dprintf.3.bz2
/usr/share/man/fr/man3/fprintf.3.bz2
/usr/share/man/fr/man3/fwprintf.3.bz2
/usr/share/man/fr/man3/printf.3.bz2
/usr/share/man/fr/man3/snprintf.3.bz2
/usr/share/man/fr/man3/sprintf.3.bz2
/usr/share/man/fr/man3/swprintf.3.bz2
/usr/share/man/fr/man3/vasprintf.3.bz2
/usr/share/man/fr/man3/vdprintf.3.bz2
/usr/share/man/fr/man3/vfprintf.3.bz2
/usr/share/man/fr/man3/vfwprintf.3.bz2
/usr/share/man/fr/man3/vprintf.3.bz2
/usr/share/man/fr/man3/vsnprintf.3.bz2
/usr/share/man/fr/man3/vsprintf.3.bz2
/usr/share/man/fr/man3/vswprintf.3.bz2
/usr/share/man/fr/man3/vwprintf.3.bz2
/usr/share/man/fr/man3/wprintf.3.bz2
/usr/share/man/man1/printf.1.bz2
/usr/share/man/man3/ber_printf.3.bz2
/usr/share/man/man3/curl_mprintf.3.bz2
</pre>


=={{header|UnixPipes}}==
=={{header|UnixPipes}}==