1. Re: Using walk_dir *without* a local variable
- Posted by Ricardo Forno <ricardoforno at t?top?a.com> Dec 01, 2007
- 470 views
Derek Parnell wrote: > > Ricardo Forno wrote: > > > Maybe I wasn't clear enough. > > Hi Ricardo, > In the past, when I needed to do this sort of thing, I also found that > walk_dir() > can't do it. It is an easy task to create a better walk_dir() though. > > Here is a version based on walk_dir... > }}} <eucode> > include file.e > include sort.e > integer SLASH > if platform() <= 2 > then SLASH = '\\' > else SLASH = '/' > end if > > global function walker(sequence path_name, > integer your_function, > integer scan_subdirs, > object user_data) > -- Generalized Directory Walker > -- Walk through a directory and (optionally) its subdirectories, > -- "visiting" each file and subdirectory. Your function will be called > -- via its routine id. The visits will occur in alphabetical order. > -- Your function should accept the path name and dir() entry for > -- each file and subdirectory. It should return 0 to keep going, > -- or an error code (greater than 0) to quit, or it can return > -- any sequence or atom other than 0 as a useful diagnostic value. > > object d > object abort_now > integer pos > > -- get the full directory information > d = dir(path_name) > if atom(d) then > return -1 > end if > > -- sort by name > d = sort(d) > > -- trim any trailing blanks or '\' characters from the path > pos = length(path_name) > while pos > 0 do > if find(path_name[pos], {' ', SLASH}) = 0 then > exit > end if > pos -= 1 > end while > path_name = path_name[1..pos-1] > > for i = 1 to length(d) do > if find('d', d[i][D_ATTRIBUTES]) then > -- a directory > if not find(d[i][D_NAME], {".", ".."}) then > abort_now = call_func(your_function, {path_name, d[i], > user_data}) > if not equal(abort_now, 0) then > return abort_now > end if > if scan_subdirs then > abort_now = walker(path_name & SLASH & d[i][D_NAME], > your_function, scan_subdirs, > user_data) > > if not equal(abort_now, 0) and > not equal(abort_now, -1) then > -- allow BAD PATH, user might delete a file or > directory > return abort_now > end if > end if > end if > else > -- a file > abort_now = call_func(your_function, {path_name, d[i], user_data}) > if not equal(abort_now, 0) then > return abort_now > end if > end if > end for > return 0 > end function > > </eucode> {{{ > > > -- > Derek Parnell > Melbourne, Australia > Skype name: derek.j.parnell Many thanks, Derek. Shouldn't this funtion be included if official EU? Regards.