Has Anyone Got A Directory "Walker" Available
Sannyasin Brahmanathaswami
brahma at hindu.org
Thu Apr 19 14:20:21 EDT 2018
Never mind…
I found this in a toolbox. Sent by some ago, by a LiveCode deva
For what it's worth :
# Filters the strings "." and ".." from a list
function filterDots pList
local tList
put pList into tList
filter tList without "."
filter tList without ".."
return tList
end filterDots
# Returns a filtered list of files in the current directory
function filteredFiles
return filterDots(the files)
end filteredFiles
# Returns a filtered list of folders in the current directory
function filteredFolders
return filterDots(the folders)
end filteredFolders
# Returns a list of files in the current directory including
# each file's full path.
function filteredFilesWithPaths
local tFiles, tFilesWithPaths
put filteredFiles() into tFiles
repeat for each line tFile in tFiles
put the directory & slash & tFile & return after \
tFilesWithPaths
end repeat
delete the last char of tFilesWithPaths
return tFilesWithPaths
end filteredFilesWithPaths
# Returns a list of files in a given folder, using recursion to
# include files in subfolders if desired.
function listFiles pFolder, pRecurse
local tTotalFiles, tCurrentFiles, tFolders
set the directory to pFolder
put filteredFiles() into tCurrentFiles
if not pRecurse then return tCurrentFiles
if tCurrentFiles is not empty then
put tCurrentFiles & return after tTotalFiles
end if
put filteredFolders() into tFolders
repeat for each line tFolder in tFolders
put listFiles((pFolder & slash & tFolder), pRecurse) into \
tCurrentFiles
if tCurrentFiles is not empty then
put tCurrentFiles & return after tTotalFiles
end if
end repeat
delete the last char of tTotalFiles
return tTotalFiles
end listFiles
# Returns a list of files with their containing folders, using
# recursion
# if desired to descend into sub folders.
function listFilesWithFolders pFolder, pRecurse
local tTotalFiles, tCurrentFiles, tFolders
set the directory to pFolder
put filteredFiles() into tCurrentFiles
if not pRecurse then return pFolder & return & "--" & return \
& tCurrentFiles
if tCurrentFiles is not empty then
put pFolder & return & "--" & return after tTotalFiles
put tCurrentFiles & return & return after tTotalFiles
end if
put filteredFolders() into tFolders
repeat for each line tFolder in tFolders
put listFilesWithFolders((pFolder & slash & tFolder), \
pRecurse) into tCurrentFiles
if tCurrentFiles is not empty then put tCurrentFiles & \
return after tTotalFiles
end repeat
delete the last char of tTotalFiles
return tTotalFiles
end listFilesWithFolders
# Returns a list of files with the full paths
function listFilesWithPaths pFolder, pRecurse
local tTotalFiles, tCurrentFiles, tFolders
set the directory to pFolder
put filteredFilesWithPaths() into tCurrentFiles
if not pRecurse then return tCurrentFiles
if tCurrentFiles is not empty then put tCurrentFiles & \
return after tTotalFiles
put filteredFolders() into tFolders
repeat for each line tFolder in tFolders
put listFilesWithPaths((pFolder & slash & tFolder), \
pRecurse) into tCurrentFiles
if tCurrentFiles is not empty then put tCurrentFiles & \
return after tTotalFiles
end repeat
delete the last char of tTotalFiles
return tTotalFiles
end listFilesWithPaths
# Returns a sorted list of the files in pFolder. Again using
# recursion if
# required.
function listSortedFiles pFolder, pRecurse
local tFiles
put listFiles(pFolder, pRecurse) into tFiles
sort lines of tFiles by listSortedFilesSortKey(each)
return tFiles
end listSortedFiles
# Used by the listSortedFiles() function. Returns a sort key
# from each file's name to
# allow the sorting to be fully customized.
function listSortedFilesSortKey pFile
# Use this value for a normal text sort
return pFile
end listSortedFilesSortKey
on doSomething pFile
put pFile & return after msg
end doSomething
--This will achieve similar results to listing the files except that it will be slower because text has to be drawn to the screen at each iteration.
# Use this template function to perform an action on each file
# in a folder
on doForEachFile pFolder, pRecurse
set the directory to pFolder
repeat for each line tFile in filteredFiles()
doSomething (pFolder & slash & tFile)
end repeat
if pRecurse then
repeat for each line tFolder in filteredFolders()
doForEachFile (pFolder & slash & tFolder), pRecurse
end repeat
end if
end doForEachFile
I have directory available this days of Ken Ray, in the on system of set the default folder and get the the files, registered that path. descend recursively to the level… etc.
Do anyone has a new one handy they could share using the folder and file function?
BR
More information about the use-livecode
mailing list