Import users from a csv file

You can share your Lua Scripts with everybody here.

Import users from a csv file

Postby FTP » Wed Nov 11, 2009 7:01 am

-- Description: import users from a csv file
-- 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
        -- next field is quoted? (start with `"'?)
        if string.find(s, '^"', fieldstart) then
          local a, c
          local i  = fieldstart
            -- 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

          table.insert(t, temp)
          fieldstart = nexti+1
      until fieldstart > string.len(s)
      return t

--get the content of csv file
local fp = assert(, "r"))
local content = fp:read("*all")

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
            attr[ii] = false
         ii = ii + 1
   --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])

print("import users successfully!")

just save the above scripts to a text file "c:/fromcsv.lua", then execute it in the web admin's console.


and the csv file's format simply like this:

Here is a example of csv file, you can download it at:
And you can also download the lua script file at:
Site Admin
posts 1257
joined Tue Sep 29, 2009 6:09 am

Re: Import users from a csv file

Postby thetigerzzz » Thu Jan 08, 2015 10:28 am

Sir first of all thanks for great script

i added 200 users from the help of this script but need little modification in my case i want to all users are the member one of one group dont need the dir path
can you plz help me out
posts 2
joined Thu Jan 08, 2015 10:21 am

Re: Import users from a csv file

Postby thetigerzzz » Sat Jan 10, 2015 6:32 am

anyone ? here for help
posts 2
joined Thu Jan 08, 2015 10:21 am

Return to Lua scripts

Who is online

Users browsing this forum: No registered users and 1 guest