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
      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")
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!")


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

Image



and the csv file's format simply like this:
Image


Here is a example of csv file, you can download it at: http://www.wftpserver.com/bbsres/ftpaccounts.csv
And you can also download the lua script file at: http://www.wftpserver.com/bbsres/fromcsv.lua
FTP
Site Admin
 
posts 1225
 
joined Tue Sep 29, 2009 6:09 am

Return to Lua scripts

Who is online

Users browsing this forum: No registered users and 2 guests