Andromo - Start Making Apps - Free Signup

Announcement

Collapse

New Forum Software

If you're here, you've found the new home for our forums. There will be some bugs to iron out, so thanks for your patience...
See more
See less

FUNCTION: FillListBoxFromFolder

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • FUNCTION: FillListBoxFromFolder

    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
    Code:
    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
    Code:
    FillListBoxFromFolderDialog( "ListBox1")

    this variation uses a predefined folder
    Code:
    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
    Code:
    
    FillListBoxFromFolder( "AutoPlay\\Docs", "ListBox1") 


    output enhanced with AMS Code Pretty

    Have fun
    Embrace change in your life, you never know, it could all work out for the best

  • #2
    This does the same as both, though I haven't tested it:

    Code:
    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;

    Comment


    • #3
      lol, ok

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

      Code:
      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
      Code:
      FillListBoxFromFolder("", "ListBox1")
      to use a predefined folder call it like this
      Code:
      FillListBoxFromFolder("AutoPlay\\Docs", "ListBox1")

      and for combo box

      Code:
      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
      Code:
      FillComboFromFolder("", "ComboBox1")
      to use a predefined folder call it like this
      Code:
      FillComboFromFolder("AutoPlay\\Docs", "ComboBox1")
      Embrace change in your life, you never know, it could all work out for the best

      Comment


      • #4
        And this also does both for both LB and CB:

        Code:
        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

        Comment


        • #5
          nice, but dose it work and whats the function call ??
          Embrace change in your life, you never know, it could all work out for the best

          Comment


          • #6
            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:

            Code:
            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

            Comment


            • #7
              Tested, made more readable:

              Code:
              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
              Last edited by Desolator; 06-12-2007, 08:16 AM.

              Comment


              • #8
                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
                Embrace change in your life, you never know, it could all work out for the best

                Comment


                • #9
                  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?

                  Comment


                  • #10
                    Great Code .
                    Last edited by fmwyso; 09-22-2007, 08:34 PM.

                    Comment

                    Working...
                    X