Recursive delete

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
berniebernie
Posts: 33
Joined: February 7th, 2005, 7:32 am
Contact:

I'm trying to delete folders & files via after effects, and I've learned the hard way that oyu need to 'empty' the folders before you can delete them.
So I'm trying to build a recursive function that will delete all the subfolders and their files as well, but it doesn't seem to handle to recursion. Any ideas ?

Code: Select all

function recursiveDelete(path){
    fold = new Folder(path.toString());
    files = fold.getFiles();
        for(file in files){
            if(files[file] instanceof Folder){
                recursiveDelete(files[file]);
            }else if(files[file] instanceof File){
               $.writeln(files[file]+ ": " +files[file].remove());
            }
        }
     fold.remove();
}
recursiveDelete("~/Desktop/deletefolder");
I know there's no error checking, but the script fails.
Boom boom boom.
berniebernie
Posts: 33
Joined: February 7th, 2005, 7:32 am
Contact:

ok, so as usual I find the answer right after posting on a forum. I guess the scope of my variable was screwing things up, until i added var before declaring variables.

This works:

Code: Select all

path = "~/Desktop/test_x";

function recursiveDelete(pathz){
    var fold = new Folder(pathz.toString());
    var files = fold.getFiles();
        for(file in files){
            
            if(files[file] instanceof Folder){
               recursiveDelete(files[file]);
            }else if(files[file] instanceof File){
               $.writeln(files[file]+": "+files[file].remove());

            }
        }
       $.writeln(fold+": "+fold.remove());
}
recursiveDelete(path);
Boom boom boom.
Post Reply