Announcement

Collapse
No announcement yet.

Folders deletion using data info from Listbox

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

  • Folders deletion using data info from Listbox

    Hi all:
    After searching in this forum and triyng myself to solve this doubt I got a partial solution to deleting folders that were found in previous searching to listbox (where is all the found folder located). The code is splitted in two parts, one code for searching folders that begings with the naming conventions "CAT-*" where * symbol is the pattern search for all folders that does it have the CAT part (those not having this pattern will be ignored and not included in listbox) in the button "search":

    Code:
    folderpath = Dialog.FolderBrowse("Please select a folder:", "d:\\");
    
    StatusDlg.Show()
    tFolders = Folder.Find(folderpath, "CAT-*", true, nil);[COLOR="seagreen"]--search for folder with naming convention whose begins with "CAT-namefolder", eg: CAT-PhotoSports[/COLOR]
    StatusDlg.Hide()
    
    for i,v in pairs(tFolders) do
    tFiles = File.Find(v, "*.*", true, false, nil, nil)
    nSize = 0;
      if tFiles ~= nil then
        for x,y in pairs(tFiles) do
        nSize = nSize+File.GetSize(y);
        end
      end
     result = Dialog.TimedMessage("Notice", "Adding folder\r\n:"..v, 10, MB_ICONINFORMATION);
    
    ListBox.AddItem("ListBox1", v, v);
    
    end
    totalFound = ListBox.GetCount("ListBox1");
    
    result = Dialog.Message("Notice", "Total found "..totalFound, MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
    Application.ExitScript();
    As for the second part code is the deletion section for those folders what begins at "CAT-xxxx" in another button called "Delete!":

    Code:
    ListBox.SelectItem("ListBox1", LB_ALLITEMS);
    
    local tbDeleteFiles = ListBox.GetSelected("ListBox1");
    if(tbDeleteFiles) then
    	for t, data in pairs (tbDeleteFiles) do
    		local strPathFileToDelete = ListBox.GetItemData("ListBox1",tbDeleteFiles[t]);
    		if(strPathFileToDelete ~= "") then
    		File.Delete(tbDeleteFiles[t].."\\*.*", false, false, false, nil);
    		Folder.DeleteTree(strPathFileToDelete, nil);
    			local errno = Application.GetLastError();
    			if (errno ~= 0) then
       			 Dialog.Message("Error", _tblErrorMessages[errno], MB_OK, MB_ICONEXCLAMATION);
       			else
    	   			ListBox.DeleteItem("ListBox1", tbDeleteFiles[t]);
    			end
    		end
    	end
    else
    	 Dialog.Message("Error", "Select an item from listbox", MB_OK, MB_ICONEXCLAMATION);
    end
    
    Dialog.Message("Notice", "Deletion successful", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
    The problem here is that listbox does not count correctly or not delete all folder found under that searching pattern, it makes me to have click again and again until all found folder gets deleted successfully. Where is my fault here? Why do not delete all folders if where found correctly in listbox. Another thing I tried is to make a while loop but i makes crash the app when is output to preview and execute. I use Win 7 x64 SP1 original and ams too version 8.3.

  • #2
    If you delete items from the ListBox, you are changing the indexes on the next iteration. Think of it like this:
    You start by deleting the folder in the first index of the ListBox, index 1. At the end, you delete that item from the ListBox, and everything scrolls one line up, so the second item is now the first.
    However, on the next iteration, you will be processing index 2 - which now points to the line which was initially the third item. So you see, the item which was initially the second one won't be deleted. And worse, you will attempt to delete indexes which no longer exist. This is the reason why you need to click the button repeatedly.

    If you need to delete the folders from the ListBox as they are processed, then you always process the first item of the ListBox. The folder is deleted, the item is removed from the ListBox, and the contents scroll one line up. Process the first item of the ListBox again, which now actually holds the data of the second item. Repeat until there are no more lines left in the ListBox.

    Ulrich

    Comment


    • #3
      okey I understand whats happening....what suggestion could you give me to fix this issue? update the listbox to get the correct items position?

      Comment


      • #4
        Hi;

        I got a solution Ulrich What do you think? It could be done in another way? The code for search button is the same not changed a lot only a fix its done:
        Code:
        folderpath = Dialog.FolderBrowse("Please select a folder:", "d:\\");
        
        StatusDlg.Show()
        tFolders = Folder.Find(folderpath, "CAT-*", true, nil);--search for folder with naming convention whose begins with "CAT-namefolder", eg: CAT-PhotoSports
        StatusDlg.Hide()
        
        if (tFolders == nil) then
        Dialog.TimedMessage("Please Wait...", "Theres not folders to search for", 2000, MB_ICONINFORMATION);
        else
        	for i,v in pairs (tFolders) do
        	tFiles = File.Find(v, "*.*", true, false, nil, nil)
        	nSize = 0;
          		if tFiles ~= nil then
            		for x,y in pairs(tFiles) do
            		nSize = nSize+File.GetSize(y);
            		end
          		end
         		result = Dialog.TimedMessage("Notice", "Adding folder\r\n:"..v, 10, MB_ICONINFORMATION);
        		
        		ListBox.AddItem("ListBox1", v .. " - " .. String.GetFormattedSize(nSize, FMTSIZE_AUTOMATIC, true), v);
        		
        	end
        		totalFound = ListBox.GetCount("ListBox1");
        	
        	Dialog.Message("Notice", "Total found "..totalFound, MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
        	Application.ExitScript();
        end
        but as for the delete button I did this:

        Code:
        ListBox.SelectItem("ListBox1", LB_ALLITEMS);
        ListBox.SetUpdate("ListBox1", false);
        count = ListBox.GetCount("ListBox1");
        
         setEmpty = false;
        
        if count == 0 then
        Dialog.TimedMessage("Please Wait...", "Select a item, it seems empty", 2000, MB_ICONINFORMATION);
        else
        
        	local tbDeleteFiles = ListBox.GetSelected("ListBox1");
        	if(tbDeleteFiles) then
        		for t, data in pairs (tbDeleteFiles) do
        		local strPathFileToDelete = ListBox.GetItemData("ListBox1",tbDeleteFiles[t]);
        		
        			if(strPathFileToDelete ~= "") then
        			File.Delete(tbDeleteFiles[t].."\\*.*", false, false, false, nil);
        			Folder.DeleteTree(strPathFileToDelete, nil);
        			end
        			Page.StartTimer(100, 10);
        		end
        	else
        	 	Dialog.Message("Error", "Select an item from listbox", MB_OK, MB_ICONEXCLAMATION);
        	end
        end
        And I added a sort of timer to verify if file was correctly deleted so I can update the list and their index in the On Timer event:

        Code:
        current = ListBox.GetSelected("ListBox1");
        setEmpty = false;
        
        if current ~= nil then
        local tbToVerifyFolder = ListBox.GetSelected("ListBox1");
        	if(tbToVerifyFolder) then
        		for n, data in pairs (tbToVerifyFolder) do
        		local strVerify = ListBox.GetItemData("ListBox1",tbToVerifyFolder[n]);
        			if(strVerify ~= "") then
        			exist = Folder.DoesExist(tbToVerifyFolder[n])
        				if exist == false then
           				ListBox.DeleteItem("ListBox1", n);
        				ListBox.SetUpdate("ListBox1", true);
           				end
        
        			end
        		end
        	end
        
        	if (ListBox.GetCount("ListBox1")) == 0 then
        	setEmpty = true;
        
        		if setEmpty == true then
        		ListBox.DeselectItem("ListBox1", -1);
        		Dialog.Message("Notice", "Deletion successful", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
        		Page.StopTimer(10);
        		end
        	end
        
        end
        This could it be better coded? I checked no errors in deletion.
        Last edited by naxo; 12-22-2014, 03:04 AM. Reason: fix a error code

        Comment

        Working...
        X