Skip to content

Commit ab7b9e9

Browse files
paths.iterfiles and paths.iterdirs
1 parent 851f314 commit ab7b9e9

File tree

2 files changed

+83
-20
lines changed

2 files changed

+83
-20
lines changed

README.md

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,34 +91,73 @@ because it makes sure that the files are removed on exit.
9191
In addition, `os.tmpname()` under windows often returns filenames
9292
for which the user has no permission to write.
9393

94-
95-
96-
9794
<a name="paths.dirs.dok"/>
9895
## Directory functions ##
9996

10097
The following functions can be used
10198
to examine directory contents or manipulate directories.
10299

103-
104100
<a name="paths.dir"/>
105101
### paths.dir(dname) ###
106102

107-
Return a table containing the files in directory `dname`.
108-
This function return `nil` if the specified directory
109-
does not exists.
103+
Return a table containing the files and directories in directory `dname`.
104+
This function return `nil` if the specified directory does not exists.
105+
For linux, this includes the `.` and `..` directories.
110106

111107
<a name="paths.files"/>
112-
### paths.files(dname) ###
108+
### paths.files(dname [, include]) ###
109+
110+
Returns an iterator over the files and directories located in directory `dname`.
111+
For linux, this includes the `.` and `..` directories.
112+
This can be used in *__for__* expression as shown below:
113+
114+
```lua
115+
for f in paths.files(".") do
116+
print(f)
117+
end
118+
```
119+
120+
Optional argument `include` is either a function or a string used to
121+
determine which files are to be included. The function takes the filename
122+
as argument and should return true if the file is to be included.
123+
When a string is provided, the following function is used :
124+
125+
```lua
126+
function(file)
127+
return file:find(f)
128+
end
129+
```
130+
131+
Files and directories of sub-folders aren't included.
132+
133+
<a name="paths.iterdirs"/>
134+
### paths.iterdirs(dname) ###
113135

114-
Returns an iterator over the files located in directory `dname`.
136+
Returns an iterator over the directories located in directory `dname`.
115137
This can be used in *__for__* expression as shown below:
138+
139+
```lua
140+
for dir in paths.iterdirs(".") do
141+
print(dir)
142+
end
116143
```
117-
for f in paths.files(".") do
118-
print(f)
119-
end
144+
145+
Directories of sub-folders, and the `.` and `..` folders aren't included.
146+
147+
<a name="paths.iterdirs"/>
148+
### paths.iterfiles(dname) ###
149+
150+
Returns an iterator over the files (non-directories) located in directory `dname`.
151+
This can be used in *__for__* expression as shown below:
152+
153+
```lua
154+
for file in paths.iterfiles(".") do
155+
print(file)
156+
end
120157
```
121158

159+
Files of sub-folders, and the `.` and `..` folders aren't included.
160+
122161
<a name="paths.mkdir"/>
123162
### paths.mkdir(s) ###
124163

init.lua

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,41 @@ else
2222
paths.home = os.getenv('HOME') or '.'
2323
end
2424

25-
function paths.files(s)
25+
function paths.files(s, f)
2626
local d = paths.dir(s)
2727
local n = 0
28+
if torch.type(f) == 'string' then
29+
local pattern = f
30+
f = function(file) return file:find(pattern) end
31+
elseif f and torch.type(f) ~= 'function' then
32+
error("Expecting optional arg 2 to be function or string. Got : "..torch.type(f))
33+
end
34+
f = f or function(file) return true end
35+
local n = 0
2836
return function()
29-
n = n + 1
30-
if (d and n <= #d) then
31-
return d[n]
32-
else
33-
return nil
34-
end
35-
end
37+
while true do
38+
n = n + 1
39+
if d == nil or n > #d then
40+
return nil
41+
elseif f(d[n]) then
42+
return d[n]
43+
end
44+
end
45+
end
46+
end
47+
48+
function paths.iterdirs(s)
49+
return paths.files(s,
50+
function(dir)
51+
return paths.dirp(paths.concat(s, dir)) and dir ~= '.' and dir ~= '..'
52+
end)
53+
end
54+
55+
function paths.iterfiles(s)
56+
return paths.files(s,
57+
function(file)
58+
return paths.filep(paths.concat(s, file)) and file ~= '.' and file ~= '..'
59+
end)
3660
end
3761

3862
function paths.thisfile(arg, depth)

0 commit comments

Comments
 (0)