Walk a directory/Recursively: Difference between revisions

(→‎{{header|UNIX Shell}}: this version does not use external programs like grep or find)
Line 34:
</lang>
The solution first enumerates files in a directory, that includes the subdirectories, if their names match the pattern. Then it steps down into each of the subdirectories. The pseudo directories . and .. are excluded. The behavior upon symbolic links depends on the [[OS]] and the implementation of the Ada.Directories package.
=={{header|ALGOL 68}}==
<!-- {{does not work with|ALGOL 68|Standard - extensions to language used}} -->
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386 - uses non-standard library routines ''get directory'' and'' grep in string''.}}
<!-- {{does not work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386 - ''get directory'' and'' grep in string'' not available in any library ... yet}} -->
<lang algol>INT match=0, no match=1, out of memory error=2, other error=3;
 
STRING slash = "/", pwd=".", parent="..";
 
PROC walk tree = (STRING path, PROC (STRING)VOID call back)VOID: (
[]STRING files = get directory(path);
FOR file index TO UPB files DO
STRING file = files[file index];
STRING path file = path+slash+file;
IF file is directory(path file) THEN
IF file NE pwd AND file NE parent THEN
walk tree(path file, call back)
FI
ELSE
call back(path file)
FI
OD
);
 
STRING re sort a68 = "[Ss]ort[^/]*[.]a68$";
 
PROC match sort a68 and print = (STRING path file)VOID:
IF grep in string(re sort a68, path file, NIL, NIL) = match THEN
print((path file, new line))
FI;
 
walk tree(".", match sort a68 and print)</lang>
Sample Output:
<pre>
./Shell_sort_c.a68
./Quick_sort.a68
./Shell_sort.a68
./Cocktail_Sort.a68
./Selection_Sort.a68
./Merge_sort.a68
./tmp/test_sort.a68
./Bobosort.a68
./Sorting_an_Array_of_Integers.a68
./Insertion_Sort.a68
./Permutation_Sort.a68
</pre>
 
=={{header|C}}==