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|D}}== |
=={{header|D}}== |
||
module std.file |
module std.file provides different walk directory functions (listdir).<br> |
||
This one recursively |
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. |
||
<java>import java.io.File; |
|||
public class MainEntry { |
|||
public static void main(String[] args) { |
|||
walkin(new File("/home/user")); //Replace this with a suitable directory |
|||
} |
|||
/** |
|||
* Recursive function to descend into the directory tree and find all the files |
|||
* that end with ".mp3" |
|||
* @param dir A file object defining the top directory |
|||
**/ |
|||
public static void walkin(File dir) { |
|||
String pattern = ".mp3"; |
|||
File listFile[] = dir.listFiles(); |
|||
if(listFile != null) { |
|||
for(int i=0; i<listFile.length; i++) { |
|||
if(listFile[i].isDirectory()) { |
|||
walkin(listFile[i]); |
|||
} else { |
|||
if(listFile[i].getName().endsWith(pattern)) { |
|||
System.out.println(listFile[i].getPath()); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
}</java> |
|||
} |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
Line 253: | Line 225: | ||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
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 |
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}}== |
||
⚫ | |||
'''Platform:''' [[.NET]] |
|||
⚫ | |||
This uses the OS pattern matching |
This uses the OS pattern matching |
||
Line 425: | Line 395: | ||
Next |
Next |
||
End Sub |
End Sub |
||
⚫ | |||
{{works with|Bourne Again SHell}} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
=={{header|UnixPipes}}== |
=={{header|UnixPipes}}== |