-
Notifications
You must be signed in to change notification settings - Fork 1
/
count.lua
64 lines (54 loc) · 1.44 KB
/
count.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
--[[
documentation: count files
usage: lua count.lua <from> <mode>
dependencies: https://keplerproject.github.io/luafilesystem/
]]
local lfs = require "lfs"
local function load_file(fileName, environment)
if setfenv and loadfile then
local f = assert(loadfile(fileName))
setfenv(f,environment)
return f
else
return assert(loadfile(fileName, "t", environment))
end
end
local donotcopy = load_file("donotcopy.lua", {mode = arg[2]})()
local counter = 1
local countFiles
countFiles = function (from)
for i=1,#donotcopy do if from:find(donotcopy[i], 0, true) then return end end
local attrFrom, errFrom = lfs.attributes(from)
if not attrFrom then return end
if attrFrom.mode == "directory" then
print("copy.lua: " .. from)
local paths = {}
for entry in lfs.dir(from) do
if entry ~= "." and entry ~= ".." then
local pathFrom, pathTo
if from ~= "/" then
if from:sub(#from,#from) == "/" then
pathFrom = from .. entry
else
pathFrom = from .. "/" .. entry
end
else
pathFrom = "/" .. entry
end
paths[#paths+1] = {from = pathFrom}
end
end
if #paths > 0 then
table.sort(paths, function(x,y) return x.from < y.from end)
for i=1,#paths do countFiles(paths[i].from) end
end
elseif attrFrom.mode == "file" then
print("count.lua: [" .. counter .. "] " .. from)
counter = counter + 1
end
end
if #arg < 1 then
print("usage: lua count.lua <from> <mode>")
return
end
countFiles(arg[1])