backup list for Bri_G

@Bri_G Try this code to create a sorted list by name and date from my backup files created from my backup program. I put this in a separate discussion because the Codea version 203 discussion is filling up with too much unrelated things. Let me know if this is or isn’t what you wanted.

displayMode(STANDARD)

function setup()
    fileTab={}
    nameTab={}
    getBkups()
    for a,b in pairs(fileTab) do
        getName(b)        
    end 
    table.sort(nameTab)
    print(table.concat(nameTab,"\
"))  
end

function getName(name)
    str=readText(asset.documents.Dropbox..name)
    st=string.find(str,"BKUP==START..",1,true)
    en=string.find(str,"\
",1,true)
    time=string.sub(str,st+13,en-1)
    ss=1
    while st~=nil do
        st=string.find(str,"PROJNAMESTART==",ss,true)
        if st~=nil then
            en=string.find(str,"==PROJNAMEEND",st,true)
            name=string.sub(str,st+15,en-1)
            table.insert(nameTab,name.."   "..time)
        end
        ss=en
    end    
end
 
function getBkups()
    local temp=asset.documents.Dropbox.all
    for a,b in pairs(temp) do
        s1=string.find(a,".txt")    -- text file
        s2=string.find(a,"bkA")     -- backup file
        if s1~=nil and s2~=nil then
            table.insert(fileTab,a)
        end 
    end 
end

@dave1707 sorry to hijack this thread but I noticed you are using asset.documents.Dropbox.all and then using the keys of the resulting table

I was planning to change this API to return an array of alphabetically sorted files in the folder. Do you think this would be the better API? In this situation your code would look like:


    for index,file in pairs(temp) do
        if file.ext == "txt" and string.find(file.name, "bkA") then
            table.insert(fileTab, file)
        end
    end 

I’d still like to make this change, would you prefer it?

@dave1707 - that’s spot on, thanks. Saved a lot of work and surprised me how many files I have and the number of projects backed up - mostly duplicated several times. Now to see if I can manually build an archive file by cutting and pasting. Hopefully I will finally get organised and be able to find what I need quickly. Thanks again.

@Simeon I didn’t like the commands that returned a keyed file where I would have to pull the key to create another table to sort. Anything that would make things easier. Your above code would work perfect, even though I would have to make code changes again to some of my projects. But that’s OK.

@Bri_G Would it be easier to restore some of the projects back into Codea and then do another backup instead of trying to manipulate a backup file. If some backup programs have the same name, you could always rename the Codea projects and then restore a backup project.

@dave1707 - at the moment not sure until I have created the file.
@Simeon - when I tried to read the table of files, in the other thread, I had a zero hit and thought it wouldn’t work. Think it was down to the filetype - there were only projects in the folder so will you be adding .codea as a filetype?

@Bri_G can you show me what code was getting zero hits? Codea projects should show up as assets, e.g., asset.documents.Examples.all should contain all example projects on your iPad

@Simeon - I posted it in Codea 3.1 (203) on the fourth page (iPad) addressed as a query to you. To save time here it is:


function setup()
    --
    lf = asset.documents.Demos.all
    num = #lf
    print(num)
end

There were 63 Codea projects in there. I tried to get the position of the array/table without specifying what file types ie left as .all.

@Bri_G I think your lf table is key sequenced and #lf doesn’t work on key sequenced tables.

Try this.

function setup()
    tab = asset.documents.Demos.all
    num = #tab
    print(num)
    for a,b in pairs(tab) do
        print(a,b)
    end
end

@dave1707 - I see, tend not to use that structure and not quite sure what key sequencing is. I assume that the data exists in a structured way but with no index. The loop structure applies an internal index. By adding num = num + 1 after the print statement and print(num) at the end you get the table/array size. Is there any other way you can get the size of the keyed data before trying to use it?

@Bri_G In a keyed sequence table, you have a key and a value. So instead of reading each value in the table until you find what you want, you use a key and it gives you the value for that key. For instance, if you have a keyed table of the elements of the periodic table, you can create information for each element. Then to get the information about an element, you just give the table the element and it returns the information. You don’t have to read the table until you find the element, you just get the info. As far as I know, there’s no way to get the size unless you read the full table and count each key. When the table is created, you can count each key being created and store the count in the table with a specific key.

@dave1707 - thanks for that, I tend to like control so the table approach is my preferred structure but I must persevere and use keyed sequence tables. Thanks for the info.

@Bri_G What I’ve been doing is reading the keyed table and creating a second table of just the keys. That way the second table is just like a regular table.

@Bri_G you can use the # operator directly on the asset library like this:

function setup()
    lf = asset.documents.Demos
    num = #lf
    print(num)
end

@Simeon The # works with asset.document.Craft.all, so it should work with others.

function setup()
    t=asset.documents.Craft.all
    print(#t)    
end

@dave1707 yes that’s new in build 204 since I made .all return an array instead of a keyed table. You can use the length operator on either the folder asset directly or on the .all table

@Simeon - just loaded 204 and amended my code to print the name property in the loop. This is very useful and powerful. Thanks for the update.