Import users from a csv file

You can share your Lua Scripts with everybody here.
FTP
Site Admin
Posts: 2072
Joined: Tue Sep 29, 2009 6:09 am

Import users from a csv file

Post by FTP »

-- 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 here: https://www.wftpserver.com/bbsres/ftpaccounts.csv
And Lua script file is here: https://www.wftpserver.com/bbsres/fromcsv.lua
thetigerzzz
Posts: 7
Joined: Thu Jan 08, 2015 10:21 am

Re: Import users from a csv file

Post by thetigerzzz »

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

Post by thetigerzzz »

anyone ? here for help
thetigerzzz
Posts: 7
Joined: Thu Jan 08, 2015 10:21 am

Re: Import users from a csv file

Post by thetigerzzz »

still waiting for help
FTP
Site Admin
Posts: 2072
Joined: Tue Sep 29, 2009 6:09 am

Re: Import users from a csv file

Post by FTP »

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
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) 
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!")
thetigerzzz
Posts: 7
Joined: Thu Jan 08, 2015 10:21 am

Re: Import users from a csv file

Post by thetigerzzz »

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 ?
FTP
Site Admin
Posts: 2072
Joined: Tue Sep 29, 2009 6:09 am

Re: Import users from a csv file

Post by FTP »

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".
vitokk
Posts: 2
Joined: Fri Jan 19, 2018 6:00 am

Re: Import users from a csv file

Post by vitokk »

how to modify the script to add "Account expiration" and FTPES, FTPS, HTTPS protocols?
FTP
Site Admin
Posts: 2072
Joined: Tue Sep 29, 2009 6:09 am

Re: Import users from a csv file

Post by FTP »

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: https://www.wftpserver.com/help/ftpserv ... _group.htm" rel="nofollow

Please check the parameters [4], [45] and [46], all have clear comment.
booster94
Posts: 2
Joined: Wed Apr 01, 2020 2:58 pm

Re: Import users from a csv file

Post by booster94 »

Can anyone verify this script still works on the latest release? Mine runs the script without error but no users are actually created, so I'm not sure what I'm missing. I'm a new user of this product.
Post Reply