Re: Help needed with walk_dir()
- Posted by PeteE Aug 03, 2010
- 1162 views
Thanks Derek and Jim, this is great!
I actually found an example on the old forum, posted by Pete Eberlein.
Wow, that was a long time ago. I don't recognize the code at all.
Problem is, I don't really understand how it works, I'll have to trace through the code a few times. I've never really got my head around routine_id() and functions like call_back(), what I really need is a sort of dummies guide to "advanced" euphoria programming.
If all you want is a single directory, you can just use the dir_oldest_first() function from my example, and ignore walk_dir() altogether. I updated it below to move the routine_id() call to a constant, since I suspect it could allocate memory each time you call it. The routine_id is used as a sort of pointer-to-a-function, that you can use to tell another function to use this function for a certain operation. The custom_sort() is a great example of this - the sorting algorithm stays the same, but you can use a custom comparison function for the items being sorted. So to sort directory items by date, we need a function that compares the date fields from two directory items, and then use the routine_id of that function with custom_sort().
include sort.e include file.e function compare_by_date(sequence file1, sequence file2) -- compare two files by date, for custom_sort return compare(file1[D_YEAR..D_SECOND], file2[D_YEAR..D_SECOND]) end function constant routine_id__compare_by_date = routine_id("compare_by_date") function dir_oldest_first(sequence path) -- Custom directory sorting function for walk_dir(). object d d = dir(path) if atom(d) then return d end if return custom_sort(routine_id__compare_by_date, d) end function
I'm using version 3.1.1, I notice that in the ver 4.0 example walk_dir() has 4 parameters and not 3...
Based on Derek's example, the global variable my_dir used by walk_dir() went away in 4.0, and it is now the 4th parameter to walk_dir()