-- The csv file has 4 data fields: 'Username','Password','Home Directory','Attributes'
-- The field 'Attributes' means directory access right, such as "1,1,1,0,1,0,0,0"
-- And "1,1,1,0,1,0,0,0" means read file=1, write file=1, append file=1, delete file=0, list directory=1, create directory=0, delete directory=0, rename file/directory=0
-- To execute this lua script, just type in a command "dofile('c:/fromcsv.lua')" with Web Admin's Console.
------------------------------------------------------------------------------------------------
-- Author: Luke
-- Date: 2009-11-11
Code: Select all
local csv_path = "c:/ftpaccounts.csv" --the csv file path
local domain = "domain1" --the domain name
--translate csv string to a table
--this is a modified function from <Programming in Lua> Chapter.20
function fromCSV(s)
s = s .. ',' -- ending comma
local t = {} -- table to collect fields
local fieldstart = 1
repeat
-- next field is quoted? (start with `"'?)
if string.find(s, '^"', fieldstart) then
local a, c
local i = fieldstart
repeat
-- find closing quote
a, i, c = string.find(s, '"("?)', i+1)
until c ~= '"' -- quote not followed by quote?
if not i then error('unmatched "') end
local f = string.sub(s, fieldstart+1, i-1)
local nexti = i+1
table.insert(t, (string.gsub(f, '""', '"')))
fieldstart = nexti + 1
else -- unquoted; find next comma
local nexti = string.find(s, ',', fieldstart)
local temp = string.sub(s, fieldstart, nexti-1)
local n = string.find(temp, '\n', 1)
if n then
temp = string.sub(temp, 1, n-1)
nexti = fieldstart+n-1
end
table.insert(t, temp)
fieldstart = nexti+1
end
until fieldstart > string.len(s)
return t
end
--get the content of csv file
local fp = assert(io.open(csv_path, "r"))
local content = fp:read("*all")
content = content.gsub(content, "\r", "")
fp:close()
local colnum = 4 --fields number of csv file
local username, password, homedir, attr
local t = fromCSV(content)
for i, s in ipairs(t) do
if i % colnum == 1 then
username = s
elseif i % colnum == 2 then
password = s
elseif i % colnum == 3 then
homedir = s
elseif i % colnum == 0 then
-- get directory attributes split by ','
attr = Split(s,",")
local ii = 1
for _,v in pairs(attr) do
if v == "1" then
attr[ii] = true
else
attr[ii] = false
end
ii = ii + 1
end
end
--not reading the firt line
if i > colnum and i % colnum == 0 then
-- add user account
c_AddUser(domain,username, md5(password), 63, 1, 1)
-- add user directory
c_AddUserDirectory(domain, username, homedir, '/', true, attr[1], attr[2], attr[3], attr[4], attr[5], attr[6], attr[7], attr[8])
end
end
print("import users successfully!")
and the csv file's format simply like this:
Here is a example of csv file, you can download it here: https://www.wftpserver.com/bbsres/ftpaccounts.csv
And Lua script file is here: https://www.wftpserver.com/bbsres/fromcsv.lua