diff options
author | Leon Bottou <leon@bottou.org> | 2015-06-03 15:03:16 +0300 |
---|---|---|
committer | Leon Bottou <leon@bottou.org> | 2015-06-03 15:03:16 +0300 |
commit | 693942c31111a8948b786f3de5cbb354024cdc52 (patch) | |
tree | c76d54e060af3c66fe5180ee442ab6630810f5c4 | |
parent | 851f31425ca3ec878cbaf516b8e3dad55164d80d (diff) | |
parent | ab7b9e9fd22bd3c51b86c9eb7c6ad3f28858d427 (diff) |
Merge pull request #8 from nicholas-leonard/dirs
paths.iterfiles and paths.iterdirs
-rw-r--r-- | README.md | 63 | ||||
-rw-r--r-- | init.lua | 40 |
2 files changed, 83 insertions, 20 deletions
@@ -91,34 +91,73 @@ because it makes sure that the files are removed on exit. In addition, `os.tmpname()` under windows often returns filenames for which the user has no permission to write. - - - <a name="paths.dirs.dok"/> ## Directory functions ## The following functions can be used to examine directory contents or manipulate directories. - <a name="paths.dir"/> ### paths.dir(dname) ### -Return a table containing the files in directory `dname`. -This function return `nil` if the specified directory -does not exists. +Return a table containing the files and directories in directory `dname`. +This function return `nil` if the specified directory does not exists. +For linux, this includes the `.` and `..` directories. <a name="paths.files"/> -### paths.files(dname) ### +### paths.files(dname [, include]) ### + +Returns an iterator over the files and directories located in directory `dname`. +For linux, this includes the `.` and `..` directories. +This can be used in *__for__* expression as shown below: + +```lua +for f in paths.files(".") do + print(f) +end +``` + +Optional argument `include` is either a function or a string used to +determine which files are to be included. The function takes the filename +as argument and should return true if the file is to be included. +When a string is provided, the following function is used : + +```lua +function(file) + return file:find(f) +end +``` + +Files and directories of sub-folders aren't included. + +<a name="paths.iterdirs"/> +### paths.iterdirs(dname) ### -Returns an iterator over the files located in directory `dname`. +Returns an iterator over the directories located in directory `dname`. This can be used in *__for__* expression as shown below: + +```lua +for dir in paths.iterdirs(".") do + print(dir) +end ``` - for f in paths.files(".") do - print(f) - end + +Directories of sub-folders, and the `.` and `..` folders aren't included. + +<a name="paths.iterdirs"/> +### paths.iterfiles(dname) ### + +Returns an iterator over the files (non-directories) located in directory `dname`. +This can be used in *__for__* expression as shown below: + +```lua +for file in paths.iterfiles(".") do + print(file) +end ``` +Files of sub-folders, and the `.` and `..` folders aren't included. + <a name="paths.mkdir"/> ### paths.mkdir(s) ### @@ -22,17 +22,41 @@ else paths.home = os.getenv('HOME') or '.' end -function paths.files(s) +function paths.files(s, f) local d = paths.dir(s) local n = 0 + if torch.type(f) == 'string' then + local pattern = f + f = function(file) return file:find(pattern) end + elseif f and torch.type(f) ~= 'function' then + error("Expecting optional arg 2 to be function or string. Got : "..torch.type(f)) + end + f = f or function(file) return true end + local n = 0 return function() - n = n + 1 - if (d and n <= #d) then - return d[n] - else - return nil - end - end + while true do + n = n + 1 + if d == nil or n > #d then + return nil + elseif f(d[n]) then + return d[n] + end + end + end +end + +function paths.iterdirs(s) + return paths.files(s, + function(dir) + return paths.dirp(paths.concat(s, dir)) and dir ~= '.' and dir ~= '..' + end) +end + +function paths.iterfiles(s) + return paths.files(s, + function(file) + return paths.filep(paths.concat(s, file)) and file ~= '.' and file ~= '..' + end) end function paths.thisfile(arg, depth) |