--@Name: "FileLib" --@Version: 1.0.1 --@Author: Luke Wigley --@LastUpdate: 13/10/04 --@Description -- Just some handy Xtra-free functions for working with local files --@History -- 1.0.1 Added folderExists function on listFiles (inDirectory, ext) -- returns a list of files (full path) in specified directory -- can supply an option extension to filter the results. If you -- want just the file names, use listFileNames below fileList = [ ] the itemDelimiter = "." if stringP(ext) then repeat with i = 1 to the maxInteger n = getNthFileNameInFolder(inDirectory , i) if n = EMPTY then exit repeat if ext contains n.item[n.item.count] then fileList.append(inDirectory & n) end if end repeat else repeat with i = 1 to the maxInteger n = getNthFileNameInFolder(inDirectory , i) if n = EMPTY then exit repeat fileList.append(inDirectory & n) end repeat end if return fileList end on listFileNames (inDirectory, ext) -- returns a list of files (just the name) in specified directory -- can supply an option extension to filter the results fileList = [ ] the itemDelimiter = "." if stringP(ext) then repeat with i = 1 to the maxInteger n = getNthFileNameInFolder(inDirectory , i) if n = EMPTY then exit repeat if ext contains n.item[n.item.count] then fileList.append(n) end if end repeat else repeat with i = 1 to the maxInteger n = getNthFileNameInFolder(inDirectory , i) if n = EMPTY then exit repeat fileList.append(n) end repeat end if return fileList end on fileExists (aPath) -- check whether a specified file exists. If no path -- information is provided, the moviePath is checked inDirectory = dirName(aPath) if inDirectory = "" then inDirectory = the moviePath basename = basename(aPath) repeat with i = 1 to the maxInteger n = getNthFileNameInFolder(inDirectory , i) if n = basename then return true if n = EMPTY then return false end repeat end on folderExists (aPath) -- use BuddyAPI if installed, otherwise use native -- functions (though cannot distinguish between file and folder) if xtraP("BudAPI") then return baFolderExists(aPath) else return fileExists (aPath) end on pathinfo (aPath) -- return some information about a file path rList = [:] rList[#dirname] = dirname(aPath) rList[#basename] = basename(aPath) rList[#extension] = extension(aPath) return rList end on dirName (aPath, sep) -- return the directory name from a path. Optional path separator can be -- specified (default is the path seperator for current OS) -- Note that if you specify a Directory path, this function will return -- the path to the enclosing folder (unlike, for example, the PHPfunction DirName) -- If aPath contains no separator, it is assumed to be a filename with no -- path information. Therefore, dirName fails (returns an empty string) if voidP(sep) then sep = the last char of the moviePath if NOT (aPath contains sep) then return "" the itemDelimiter = sep if the last char of aPath = sep then delete the last char of aPath mx = aPath.item.count if mx > 1 then return (aPath.item[1..mx-1] & sep) return (aPath & sep) end on basename (aPath, sep) -- return the base file name from a path. Optional path separator can be -- specified (default is the path seperator for current OS) if voidP(sep) then sep = the last char of the moviePath the itemDelimiter = sep if the last char of aPath = sep then return "" else return (aPath.item[aPath.item.count]) end on extension (aPath) -- return the file extension from a filename or a file path. sep = the last char of the moviePath if aPath contains sep then aPath = baseName(aPath) the itemDelimiter = "." if (aPath contains ".") then return return aPath.item[aPath.item.count] else return "" end