updating oE walk_dir
walk_dir
include filesys.e namespace filesys public function walk_dir(sequence path_name, object your_function, integer scan_subdirs = types :FALSE, object dir_source = types :NO_ROUTINE_ID)
Generalized Directory Walker
Parameters:
- path_name : a sequence, the name of the directory to walk through
- your_function : the routine id of a function that will receive each path returned from the result of dir_source, one at a time. Optionally, to include extra data for your function, your_function can be a 2 element sequence, with the routine_id as the first element and other data as the second element.
- scan_subdirs : an optional integer, 1 to also walk though subfolders, 0 (the default) to skip them all.
- dir_source : an optional integer. A routine_id of a user-defined routine that returns the list of paths to pass to your_function. If omitted, the dir() function is used. If your routine requires an extra parameter, dir_source may be a 2 element sequence where the first element is the routine id and the second is the extra data to be passed as the second parameter to your function.
Returns:
An object,
- 0 on success
- W_BAD_PATH an error occurred
- anything else the custom function returned something to stop walk_dir.
Comments:
This routine will "walk" through a directory named path_name. For each entry in the directory, it will call a function, whose routine_id is your_function. If scan_subdirs is non-zero (TRUE), then the subdirectories in path_name will be walked through recursively in the very same way.
The routine that you supply should accept two sequences, the path name and dir entry for each file and subdirectory. It should return 0 to keep going, W_SKIP_DIRECTORY to avoid scan the contents of the supplied path name (if a directory), or non-zero to stop walk_dir. Returning W_BAD_PATH is taken as denoting some error.
This mechanism allows you to write a simple function that handles one file at a time, while walk_dir handles the process of walking through all the files and subdirectories.
By default, the files and subdirectories will be visited in alphabetical order. To use a different order, use the dir_source to pass the routine_id of your own modified dir function that sorts the directory entries differently.
The path that you supply to walk_dir must not contain wildcards (* or ?). Only a single directory (and its subdirectories) can be searched at one time.
For Windows systems, any '/' characters in path_name are replaced with '\'.
All trailing slash and whitespace characters are removed from path_name.
Example 1:
function look_at(sequence path_name, sequence item) -- this function accepts two sequences as arguments -- it displays all C/C++ source files and their sizes if find('d', item[D_ATTRIBUTES]) then -- Ignore directories if find('s', item[D_ATTRIBUTES]) then return W_SKIP_DIRECTORY -- Don't recurse a system directory else return 0 -- Keep processing as normal end if end if if not find(fileext(item[D_NAME]), {"c","h","cpp","hpp","cp"}) then return 0 -- ignore non-C/C++ files end if printf(STDOUT, "%s%s%s: %d\n", {path_name, {SLASH}, item[D_NAME], item[D_SIZE]}) return 0 -- keep going end function function mysort(sequence path) object d d = dir(path) if atom(d) then return d end if -- Sort in descending file size. return sort_columns(d, {-D_SIZE}) end function exit_code = walk_dir("C:\\MYFILES\\", routine_id("look_at"), TRUE, routine_id("mysort"))
See Also:
Not Categorized, Please Help
|