Skip to main content
Participant
October 24, 2021
Question

addFolder script only create folder once

  • October 24, 2021
  • 2 replies
  • 170 views

I'm making a script that creates a "hero" folder, a "clips" folder and moves the footage into the "clips" folder, and the "clips" folder to the "hero" folder.

My problem is that it creates multiple "hero" folders for footage that has the same name. I need to implement a check if the "hero" folder already exists in my project, I've been trying all day but got nowhere, can someone help out?

var items = app.project.selection;

    for (var i = 0; i < items.length; i++) {
        //create Folder
        var heroName = item.name.split(' ')[0];
        var heroFolder = app.project.items.addFolder(heroName);
        var clipFolder = app.project.items.addFolder(item.name);
        heroFolder.parentFolder = items[i].parentFolder;
        clipFolder.parentFolder = heroFolder;
            //move Source Footage
        items[i].parentFolder = clipFolder; 
}

 

This topic has been closed for replies.

2 replies

Meng Zhiqun
Inspiring
October 29, 2021

If you are doing a loop and adding stuff, it's better to do a reverse loop. That way, newly created items do not get added to the total length for the loop. Something like this.
for (var i = items.length ; i >0; i--) {


Mathias Moehl
Community Expert
Community Expert
October 26, 2021

The best solution is to write a function like

getOrCreateFolder(name){

/*

iterate over all items of the root folder and check if one of them is a folder and has the given name.
If so, return this folder, otherwise create a new folder and return it

*/

}

Then use this function instead of your addFolder call.

 

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects