Walk a directory/Recursively: Difference between revisions

Content added Content deleted
Line 1,122: Line 1,122:


=={{header|LiveCode}}==
=={{header|LiveCode}}==
<lang LiveCode>function recurDir dir, ext
This implementation only lists folders with files that match the specified pattern (use * to match all).
set the defaultFolder to dir
<lang LiveCode>function listfiles d, ext
repeat for each line fi in the files
put dirwalk(d) into fileDirList
if fileExt(fi) = ext then
repeat for each line fd in fileDirList
set the defaultFolder to fd
put the longfilepath of fi & cr after fileList
put the files into tmp
filter tmp with ext
if the number of lines of tmp > 0 then
repeat with i = 1 to the number of lines of tmp
put fd & slash before line i of tmp
end repeat
put tmp & cr after filelist
end if
end if
end repeat
end repeat
repeat for each line di in the folders
filter filelist without empty
if di is not "." and di is not ".." then
return filelist
put recurDir((dir & slash & di), ext) & cr after fileList
end listfiles
end if
end repeat
filter fileList without empty
return fileList
end recurDir


function dirwalk d
function fileExt f
set the defaultfolder to d
set the itemdel to "."
put the folders into dirlist
return the last item of f
end fileExt</lang>
filter dirlist without ".*"
filter dirlist without "$*"
if the number of lines of dirlist > 0 then
repeat with i = 1 to the number of lines of dirlist
put d & slash before line i of dirlist
put dirwalk(line i of dirlist) after dirlist
end repeat
end if
return dirlist
end dirwalk</lang>
Example
Example
<lang LiveCode>put listfiles(the home folder & slash & "music", "*.mp*")</lang>
<lang LiveCode>put recurDir(the home folder & slash & "music", "mp3")</lang>
Output
Output
<pre>... /Users/xxx/music/albumx/trackx.mp3
<pre>... /Users/xxx/music/albumx/trackx.mp3