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 1564
 
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
thetigerzzz
 
posts 7
 
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
thetigerzzz
 
posts 7
 
joined Thu Jan 08, 2015 10:21 am

Re: Import users from a csv file

Postby thetigerzzz » Fri Feb 06, 2015 6:01 am

still waiting for help
thetigerzzz
 
posts 7
 
joined Thu Jan 08, 2015 10:21 am

Re: Import users from a csv file

Postby FTP » Mon Feb 09, 2015 2:47 am

thetigerzzz wrote: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


OK, you just need to add a new column "Group":

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 = 5   --fields number of csv file
local username, password, homedir, groups, 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 == 4 then
      groups = {}
      local arrayGroup = Split(s,",")
      for _, strGroup in ipairs(arrayGroup) do
         table.insert(groups,{strGroup})
      end
   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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,groups,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
   end
end

print("import users successfully!")
FTP
Site Admin
 
posts 1564
 
joined Tue Sep 29, 2009 6:09 am

Re: Import users from a csv file

Postby thetigerzzz » Tue Feb 17, 2015 9:20 am

first of all thx a lot
for using this script i want to change Attributes 1,0,0,0,1,0,0,0 column to Group Name ?
thetigerzzz
 
posts 7
 
joined Thu Jan 08, 2015 10:21 am

Re: Import users from a csv file

Postby FTP » Wed Feb 18, 2015 8:26 am

thetigerzzz wrote:first of all thx a lot
for using this script i want to change Attributes 1,0,0,0,1,0,0,0 column to Group Name ?


Just add a new column "Group".
FTP
Site Admin
 
posts 1564
 
joined Tue Sep 29, 2009 6:09 am

Re: Import users from a csv file

Postby vitokk » Fri Jan 19, 2018 6:40 am

how to modify the script to add "Account expiration" and FTPES, FTPS, HTTPS protocols?
vitokk
 
posts 2
 
joined Fri Jan 19, 2018 6:00 am

Re: Import users from a csv file

Postby FTP » Tue Jan 23, 2018 6:38 am

vitokk wrote:how to modify the script to add "Account expiration" and FTPES, FTPS, HTTPS protocols?


So please check the above post about adding the groups, also check out the API description for "C_AddUser" here: http://www.wftpserver.com/help/ftpserver/user__group.htm

Please check the parameters [4], [45] and [46], all have clear comment.
FTP
Site Admin
 
posts 1564
 
joined Tue Sep 29, 2009 6:09 am


Return to Lua scripts

Who is online

Users browsing this forum: No registered users and 1 guest