Prompt For Folder (code snippet)

Description

Function which can be called to Prompt the user for a sub-folder, for example where asking for a folder with-in the Media folder, it can either be picked from the list or the Add button pressed to create a new folder with in the selected one.

Requires: lfs

Code

function folderSelection(dir_path,sTreeTitle)
local sDir = ''
local function get_dir (dir_path)
  local files = {}
  local dirs = {}
  for f in lfs.dir(dir_path) do
    if f ~= '.' and f ~= '..' then
      if lfs.attributes(dir_path..'\\'..f,'mode') == 'file' then
 
      else
        table.insert(dirs,f)
      end
    end
  end
  return files,dirs
end
 
 
 
local tree = iup.tree {}
tree.name = sTreeTitle
tree.addexpanded = "NO"
 
local lbl_selected = iup.text { value = "\\", expand="HORIZONTAL",PADDING='5x5' }
 
local btn_ok = iup.button{ title = "OK",expand="HORIZONTAL", size='20x20',action=function() sDir=lbl_selected.value return iup.CLOSE end} 
 
local function btn_add_action()
    sFolder = lbl_selected.value
    sPrompt = "Please select new Sub folder name for "..sFolder.."%t\nNew Sub Folder: %s\n"
 
    ret, sName = iup.GetParam("Please select new Sub folder name for "..sFolder,
    param_action,
    sPrompt,
    '',
    0)
	 print(ret)
    if ret then
		local sNewFolder = dir_path..sFolder..'\\'..sName
    	lfs.mkdir(sNewFolder)
		return true
    end	
end
 
local btn_add = iup.button{ title = 'Add New Folder in "'..sTreeTitle..'" and select',expand="HORIZONTAL", size='20x20',action=function () if btn_add_action() then return iup.CLOSE end end}
local function set (id,value,attrib)
  iup.TreeSetUserId(tree,id,{value,attrib})
end
 
local function get(id)
  return iup.TreeGetUserId(tree,id)
end
 
local function fill (dir_path,id)
  local files,dirs = get_dir(dir_path)
  id = id + 1
  local state = "STATE"..id
 
  for i = #dirs,1,-1 do
    tree.addbranch = dirs[i]
    set(id,dir_path..'\\'..dirs[i],'dir')
    tree['addleaf'..id] = "dummy" -- add a dummy node so branchopen_cb can be called
  end
end
 
function tree:selection_cb(id)
	local t=get(id)
 
	if t then	
	  lbl_selected.value = t[1]:sub(dir_path:len() + 2)
		btn_add.title = 'Add New Folder in "'..lbl_selected.value..'" and select'
	else
	  lbl_selected.value = '\\'
		btn_add.title = 'Add New Folder in "'..sTreeTitle..'" and select'
 	end
end
function tree:branchopen_cb(id)
  tree.value = id
  print(id)
  local t = get(id)
 
  if t and t[2] == 'dir' then
    tree['delnode'..id+1] = 'selected' -- remove dummy
    fill(t[1],id)
    set(id,t[1],'xdir') -- mark branch as filled
  end
end
 
 
local dlg = iup.dialog{iup.vbox{
		tree,
	   iup.label({title='Please select the folder above and click OK',PADDING='5x5'}),
		lbl_selected,
		btn_ok,
		iup.label({title='or use the button below to use a new sub folder',PADDING='5x5'}),
		btn_add;MARGIN='5x5'
	}; title = sTreeTitle.." Folder Selection";size='400x300'}
 
iup.SetHandle("main", dlg)
dlg:map()
fill(dir_path,0)
 
tree.STATE0 = 'EXPANDED'
dlg:show()
 
if (iup.MainLoopLevel()==0) then iup.MainLoop() end
	dlg:destroy()
	return sDir
end

Call be called as follows Parameter 1: String the base folder Parameter 2: String the short name for the folder selected, used as the tree title.

-- Require File system management
require 'lfs'
 
local sFolder = folderSelection(fhGetContextInfo('CI_PROJECT_DATA_FOLDER')..'\\Media\\','Media')
if sFolder
  print(sFolder)
else
  print('aborted')
end

Prompt Image

Section plugins:code_snippets