PDA

View Full Version : FUNCTION: FillListBoxFromFolder



RizlaUK
06-11-2007, 08:32 AM
Hi Guys

heres is 2 variations of a small function that will fill a listbox with the contents of the selected folder

this variation will present the user with a folder browse dialog and load the files from the selected folder


function FillListBoxFromFolderDialog(LB)
folder = Dialog.FolderBrowse( "Open Folder", _DesktopFolder);
if folder ~= "CANCEL" then
ListBox.SetUpdate(LB, false);
ListBox.DeleteItem(LB, -1);
-- Set Default Extension
-- tbSearchFiles = {"*.doc", "*.ppt", "*.odt", "*.exe"}; set your own file types here
-- this one search all types
tbSearchFiles = { "*.*"};
for index,type in tbSearchFiles do
file = File.Find(folder, type, false, false, nil);
if file ~= nil then
for j,file_path in file do
tbFilePath = String.SplitPath(file_path);
sFile = tbFilePath.Filename..tbFilePath.Extension;
ListBox.AddItem(LB, sFile, file_path);
end
end
end
ListBox.SetUpdate(LB, true);
end
end

and call the function with the listbox name


FillListBoxFromFolderDialog( "ListBox1")


this variation uses a predefined folder


function FillListBoxFromFolder(folder, LB)
isfolder = Folder.DoesExist(folder);
if isfolder then
ListBox.SetUpdate(LB, false);
ListBox.DeleteItem(LB, -1);
-- Set Default Extension
-- tbSearchFiles = {"*.doc", "*.ppt", "*.odt", "*.exe"}; set your own file types here
-- this one search all types
tbSearchFiles = { "*.*"};
for index,type in tbSearchFiles do
file = File.Find(folder, type, false, false, nil);
if file ~= nil then
for j,file_path in file do
tbFilePath = String.SplitPath(file_path);
sFile = tbFilePath.Filename..tbFilePath.Extension;
ListBox.AddItem(LB, sFile, file_path);
end
end
end
ListBox.SetUpdate(LB, true);
end
end

and call this function with the target folder and the listbox name


FillListBoxFromFolder( "AutoPlay\\Docs", "ListBox1")

output enhanced with AMS Code Pretty (http://www.indigorose.com/forums/showthread.php?t=19409)

Have fun

Desolator
06-11-2007, 02:25 PM
This does the same as both, though I haven't tested it:


function ListBoxAddFolder(strListBox, strFolderPath)
if not strFolderPath then
strFolderPath = Dialog.FolderBrowse("Open Folder", _DesktopFolder);
end

ListBox.SetUpdate(LB, false);
ListBox.DeleteItem(LB, -1);

local tblSearchFiles = { "*.*"};

for numSearchIndex, strFile in tblSearchFiles do
local tblFoundFiles = File.Find(strFolderPath, strFile, true, false, nil);

if tblFoundFiles then
for numFileIndex, strFilePath in tblFoundFiles do
tblFilePath = String.SplitPath(strFilePath);
ListBox.AddItem(strListBox, tblFilePath.Filename .. tblFilePath.Extension, strFilePath);
end
end
end

ListBox.SetUpdate(LB, true);
end

ListBox.AddFolder = ListBoxAddFolder;

RizlaUK
06-11-2007, 02:56 PM
lol, ok

heres the same function that dose both...(and it is tested)


function FillListBoxFromFolder(folder, LB)

if folder == "" then
folder = Dialog.FolderBrowse("Open Folder", _DesktopFolder);
end

isfolder = Folder.DoesExist(folder);
if isfolder then
ListBox.SetUpdate(LB, false);
ListBox.DeleteItem(LB, -1);
-- Set Default Extension
-- tbSearchFiles = {"*.doc", "*.ppt", "*.odt", "*.exe"}; set your own file types here
-- this one search all types
tbSearchFiles = {"*.*"};
for index,type in tbSearchFiles do
file = File.Find(folder, type, false, false, nil);
if file ~= nil then
for j,file_path in file do
tbFilePath = String.SplitPath(file_path);
sFile = tbFilePath.Filename..tbFilePath.Extension;
ListBox.AddItem(LB, sFile, file_path);
end
end
end
ListBox.SetUpdate(LB, true);
end
end

to make it show the folder browse dialog call the function like this

FillListBoxFromFolder("", "ListBox1")

to use a predefined folder call it like this

FillListBoxFromFolder("AutoPlay\\Docs", "ListBox1")


and for combo box


function FillComboFromFolder(folder, CB)

if folder == "" then
folder = Dialog.FolderBrowse("Open Folder", _DesktopFolder);
end

isfolder = Folder.DoesExist(folder);
if isfolder then
ComboBox.SetUpdate(CB, false);
ComboBox.ResetContent(CB);
-- Set Default Extension
-- tbSearchFiles = {"*.doc", "*.ppt", "*.odt", "*.exe"}; set your own file types here
-- this one search all types
tbSearchFiles = {"*.*"};
for index,type in tbSearchFiles do
file = File.Find(folder, type, false, false, nil);
if file ~= nil then
for j,file_path in file do
tbFilePath = String.SplitPath(file_path);
sFile = tbFilePath.Filename..tbFilePath.Extension;
ComboBox.AddItem(CB, sFile, file_path);
end
end
end
ComboBox.SetUpdate(CB, true);
end
end


to make it show the folder browse dialog call the function like this

FillComboFromFolder("", "ComboBox1")

to use a predefined folder call it like this

FillComboFromFolder("AutoPlay\\Docs", "ComboBox1")

Desolator
06-12-2007, 01:22 AM
And this also does both for both LB and CB:


function FillListFromFolder(folder, obj)
if Page.GetObjectType("Cmd_Input") == OBJECT_LISTBOX then
if folder == "" then
folder = Dialog.FolderBrowse("Open Folder", _DesktopFolder);
end

isfolder = Folder.DoesExist(folder);
if isfolder then
ListBox.SetUpdate(obj, false);
ListBox.DeleteItem(obj, -1);
-- Set Default Extension
-- tbSearchFiles = {"*.doc", "*.ppt", "*.odt", "*.exe"}; set your own file types here
-- this one search all types
tbSearchFiles = {"*.*"};
for index,type in tbSearchFiles do
file = File.Find(folder, type, false, false, nil);
if file ~= nil then
for j,file_path in file do
tbFilePath = String.SplitPath(file_path);
sFile = tbFilePath.Filename..tbFilePath.Extension;
ListBox.AddItem(obj, sFile, file_path);
end
end
end
ListBox.SetUpdate(obj, true);
end
elseif Page.GetObjectType("Cmd_Input") == OBJECT_COMBOBOX then
if folder == "" then
folder = Dialog.FolderBrowse("Open Folder", _DesktopFolder);
end

isfolder = Folder.DoesExist(folder);
if isfolder then
ComboBox.SetUpdate(obj, false);
ComboBox.ResetContent(obj);
-- Set Default Extension
-- tbSearchFiles = {"*.doc", "*.ppt", "*.odt", "*.exe"}; set your own file types here
-- this one search all types
tbSearchFiles = {"*.*"};
for index,type in tbSearchFiles do
file = File.Find(folder, type, false, false, nil);
if file ~= nil then
for j,file_path in file do
tbFilePath = String.SplitPath(file_path);
sFile = tbFilePath.Filename..tbFilePath.Extension;
ComboBox.AddItem(obj, sFile, file_path);
end
end
end
ComboBox.SetUpdate(obj, true);
end
end
end

RizlaUK
06-12-2007, 05:14 AM
nice, but dose it work and whats the function call ??

Desolator
06-12-2007, 07:50 AM
Same as yours:
FillListFromFolder("C:\My Folder", "ListBox1");
FillListFromFolder("C:\My Folder", "ComboBox1");

It detects the type automatically...well, if I didn't miss something:


function FillListFromFolder(folder, obj)
if Page.GetObjectType(obj) == OBJECT_LISTBOX then
if folder == "" then
folder = Dialog.FolderBrowse("Open Folder", _DesktopFolder);
end

isfolder = Folder.DoesExist(folder);
if isfolder then
ListBox.SetUpdate(obj, false);
ListBox.DeleteItem(obj, -1);
-- Set Default Extension
-- tbSearchFiles = {"*.doc", "*.ppt", "*.odt", "*.exe"}; set your own file types here
-- this one search all types
tbSearchFiles = {"*.*"};
for index,type in tbSearchFiles do
file = File.Find(folder, type, false, false, nil);
if file ~= nil then
for j,file_path in file do
tbFilePath = String.SplitPath(file_path);
sFile = tbFilePath.Filename..tbFilePath.Extension;
ListBox.AddItem(obj, sFile, file_path);
end
end
end
ListBox.SetUpdate(obj, true);
end
elseif Page.GetObjectType(obj) == OBJECT_COMBOBOX then
if folder == "" then
folder = Dialog.FolderBrowse("Open Folder", _DesktopFolder);
end

isfolder = Folder.DoesExist(folder);
if isfolder then
ComboBox.SetUpdate(obj, false);
ComboBox.ResetContent(obj);
-- Set Default Extension
-- tbSearchFiles = {"*.doc", "*.ppt", "*.odt", "*.exe"}; set your own file types here
-- this one search all types
tbSearchFiles = {"*.*"};
for index,type in tbSearchFiles do
file = File.Find(folder, type, false, false, nil);
if file ~= nil then
for j,file_path in file do
tbFilePath = String.SplitPath(file_path);
sFile = tbFilePath.Filename..tbFilePath.Extension;
ComboBox.AddItem(obj, sFile, file_path);
end
end
end
ComboBox.SetUpdate(obj, true);
end
end
end

Desolator
06-12-2007, 08:12 AM
Tested, made more readable:



01 function FillListFromFolder(strObject, strFolder)
02 local tblSearchFiles = { "*.*"};
03 local numObjectType = Page.GetObjectType(strObject);
04
05 if numObjectType == 9 then
06 if not strFolder then
07 strFolder = Dialog.FolderBrowse( "Open Folder", _DesktopFolder);
08 end
09
10 if Folder.DoesExist(strFolder) then
11 ListBox.SetUpdate(strObject, false);
12 ListBox.DeleteItem(strObject, -1);
13
14 for numIndex, strFile in tblSearchFiles do
15 tblFilesFound = File.Find(strFolder, strFile, true, false, nil);
16
17 if tblFilesFound then
18 for numIndex, strFileFound in tblFilesFound do
19 local tblFilePath = String.SplitPath(strFileFound);
20 ListBox.AddItem(strObject, tblFilePath.Filename .. tblFilePath.Extension, strFileFound);
21 end
22 end
23 end
24
25 ListBox.SetUpdate(strObject, true);
26 end
27 elseif numObjectType == 10 then
28 if not strFolder then
29 strFolder = Dialog.FolderBrowse( "Open Folder", _DesktopFolder);
30 end
31
32 if Folder.DoesExist(strFolder) then
33 ComboBox.SetUpdate(strObject, false);
34 ComboBox.ResetContent(strObject);
35
36 for numIndex, strFile in tblSearchFiles do
37 tblFilesFound = File.Find(strFolder, strFile, true, false, nil);
38
39 if tblFilesFound then
40 for numIndex, strFileFound in tblFilesFound do
41 local tblFilePath = String.SplitPath(strFileFound);
42 ComboBox.AddItem(strObject, tblFilePath.Filename .. tblFilePath.Extension, strFileFound);
43 end
44 end
45 end
46
47 ComboBox.SetUpdate(strObject, true);
48 end
49 end
50 end

output enhanced with AMS Code Pretty (http://www.indigorose.com/forums/showthread.php?t=19409)

RizlaUK
06-12-2007, 10:12 AM
just a lil hint, when using AMS Code Pretty to post code in the forum you should disable the numbering, it just makes it easyer to c&p the code :yes

Desolator
06-12-2007, 11:51 AM
Thanks for the tip. I'd make the function for the Tree Object, but the example is kinda confusing, not well-structured. Could you give it a shot, please?

fmwyso
09-22-2007, 08:21 PM
Great Code :D.