Check for new subfolder and delete the oldest one?

Post your questions and help other users.

Moderator: Martin

Post Reply
Freakadude
Posts: 25
Joined: 20 Sep 2013 08:59

Check for new subfolder and delete the oldest one?

Post by Freakadude » 17 Jan 2014 11:40

Hi,

So what i want to achieve is that via Rom Manager i have scheduled to create Nandroid backups of my phone every 4 days or so. As my nandroid backups tend to be rather large 6GB+ or soI want to create a flow that checks if there is new subdirectory (which contains the new nandroid backup) and if yes then to delete the previously stored backup on my phone so that at any given time i allways only have one huge ass nandroid backup on my phone which should be the latest one.

So what i did so far is i can setup the event trigger File Observer to check for Subfile created so it looks at the Nandroid backup directory if a new subdirectory has been created.

Now this is the point where i am stuck as i do not find a condition or action type which would allow me to setup check to look for the oldest subdirectory (based on timestamp i guess) and then delete it and leave the other one in tact.

Any ideas how i could achieve this? I guess this could be done with a script maybe or something but i am really not confortable in doing those yet.

Any help would be appriciated.

Thanks

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Check for new subfolder and delete the oldest one?

Post by Martin » 17 Jan 2014 13:00

Hi,

There's no direct way to check the modification date of a folder in Automagic.
You could probably create a flow with action Init Variable File List and action Delete File but it might require a small script.

What are the names of the folders containing the backups? How many backups would you like to retain?

Regards,
Martin

Freakadude
Posts: 25
Joined: 20 Sep 2013 08:59

Re: Check for new subfolder and delete the oldest one?

Post by Freakadude » 17 Jan 2014 13:08

Hi Martin,

Typically i want to have one nandroid backup on my phone. So if Rom manager runs every 4 days it would create a second one thus making the previous one obsolete.

Backups are generated and stored into the default TWRP directory which for me is: /storage/emulated/0/TWRP/BACKUPS/FA36NW903726/. This is what the file observation trigger is also checking.

The name of the subdirectory in which the new nandroid backup is stored is by default the date and time when rom manager is creating the backup. I.e.: 2014-01-17-14.05.07

Thanks

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Check for new subfolder and delete the oldest one?

Post by Martin » 17 Jan 2014 15:24

Luckily the folder names of the backup subdirectory contains the date in format yyyy-MM-dd-HH... so the directories can be sorted by name.
The flow needs a bit of scripting. Something like this should work (untested):

1) trigger ...

2) action Init Variable File List: files to /storage/emulated/0/TWRP/BACKUPS/FA36NW903726/*. This action will list the backup subdirectories and stores the list in variable files.
For example:
/storage/emulated/0/TWRP/BACKUPS/FA36NW903726/2014-01-10...
/storage/emulated/0/TWRP/BACKUPS/FA36NW903726/2014-01-14...
/storage/emulated/0/TWRP/BACKUPS/FA36NW903726/2014-01-18...

3) action Script. (Text after // is a comment and is not executed)

Code: Select all

// store the number of directories in a variable
listlength = length(files);

// check that there is at least one directory
if (listlength>0) 
{
    // remove the last (newest) directory from the list (listlength-1 is the position of the last element)
    files = removeElement(files, listlength - 1);
}
At this point the variable files contains all old directories you want to delete (or no directories when there was only one directory that should be retained).

4) condition Expression: length(files)>0. Checks if the flow has to delete at least one directory.

5) --> true: -action Delete Files: {files,listformat,comma}. Deletes all directories in variable files
I highly recommend to use a placeholder for step 5 and test whether the flow really deletes the old directories. You could use a placeholder action like Notification on Screen: {files,listformat} to show the list of files on screen.

It's also very useful to add condition Debug Dialog after each action to show the values of all variables in a flow and to be able to test the flow step by step. Selecting Yes continues the flow on the true-branch of the condition, selecting No continues on the false-branch.

Regards,
Martin

Freakadude
Posts: 25
Joined: 20 Sep 2013 08:59

Re: Check for new subfolder and delete the oldest one?

Post by Freakadude » 17 Jan 2014 15:28

Thank you Martin! I will try this out over the next and let you know of the results if it worked or not.

Thanks again!

Freakadude
Posts: 25
Joined: 20 Sep 2013 08:59

Re: Check for new subfolder and delete the oldest one?

Post by Freakadude » 22 Jan 2014 09:13

Hi Martin,

I have tryed out your suggestion and it works to some extent. I have used dummy folders (to test the actual delete action) which were the same naming format as regular ones. The flow once initiated did its job and deleted 1 of the two folders.

There are two issues:

1) The folder that was deleted out of the two was actually the latest folder and not the older one.
2) If the flow is re-triggered once more then the flow also deletes the second folder as well. So the logic which should be that at least one folder should remain does not seem to work.

Actually Rom Manager when executing scheduled backups names the folders the following way: ScheduledBackup-2014-01-22-01.00.00

Don't think the naming should be an issue since its pretty standartized and still contains the date formating.

One additional bug i noticed is in the trigger. I have used File Observer with Subfile created which i assumed once the backup is done and the phone reboots Automagic should notice the newly created backup dir and therefore initiate the flow. However i noticed that the flow does not initiate after the phone has booted up again. So to counter this i added a second trigger Automagic Started which seemed to solve this issue but then i noticed another bug which is that lets say i delete a folder manually via a file manager program from within the Observed directory the flow is initiated which shouldn't happen as the trigger is set only to look for Subfile created and not Subfile deleted action.

Appriciate your help!

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Check for new subfolder and delete the oldest one?

Post by Martin » 22 Jan 2014 19:19

Hi,

Please post the flow or the send the flow to info@automagic4android.com so I can check if there's something wrong (on the flow edit screen: menu->Send Flow).

What device model, Android version and ROM are you using?

Regards,
Martin

Freakadude
Posts: 25
Joined: 20 Sep 2013 08:59

Re: Check for new subfolder and delete the oldest one?

Post by Freakadude » 22 Jan 2014 20:31

Hi Martin,

Uploaded the flow to Dropbox. You can check it via the below shared link.

https://www.dropbox.com/s/ywb3usrjw6kfw ... 212806.xml

Rom: ViperOne 4.3.0
Device Model: HTC One

Thanks for checking

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Check for new subfolder and delete the oldest one?

Post by Martin » 22 Jan 2014 21:05

Indeed, there's a bug in the script :oops:

In step 3 the assignment is wrong.
replace this line:
files = removeElement(files, listlength - 1);
by:
removeElement(files, listlength - 1);

The entire script of step 3 becomes this:

Code: Select all

// store the number of directories in a variable
listlength = length(files);

// check that there is at least one directory
if (listlength>0) 
{
    // remove the last (newest) directory from the list (listlength-1 is the position of the last element)
    removeElement(files, listlength - 1);
}
I can not reproduce the File Observer problem. Deleting a folder with a file manager does not execute the trigger. What file manager did you use?
Could you please enable the flow (or a new flow that just contains the trigger File Observer), delete a folder in the file manager then send the log file to info@automagic4android.com (on the flow list: menu->Manage->Send Log)?

Thanks
Martin

Freakadude
Posts: 25
Joined: 20 Sep 2013 08:59

Re: Check for new subfolder and delete the oldest one?

Post by Freakadude » 22 Jan 2014 21:29

Thanks Martin! Now it works flawlessly :D

I also couldn't reproduce the trigger error now so for the moment lets leave it. If it happens again i will let you know and also post the log.

Thanks again for your help with this flow! :)

Post Reply