Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Check if folder exists

  • Check if folder exists

    Posted by Avinash Ramanath on February 2, 2019 at 2:05 am

    I want to check if a folder with name CNM already exists, else create it and avoid duplication, but I don’t know how. Kindly help.

    for (var i = 1; i <= app.project.items.length; i++)
    if ((app.project.items[i] instanceof FolderItem) && (app.project.items[i].name === "CNM")
    && (app.project.items[i] != "undefined")) {
    break;
    }
    else{
    alert("not found");
    //app.project.items.addFolder("CNM");
    }

    Avinash Ramanath replied 5 years, 10 months ago 3 Members · 5 Replies
  • 5 Replies
  • Oleg Pirogov

    February 2, 2019 at 10:09 am

    What seem to be the problem? The script works like you want it to as it is, except for adding break after addFolder *when you uncomment it):
    alert("not found");
    app.project.items.addFolder("CNM");
    break;

    and fixing this:
    i <= app.project.items.length;

  • Andrei Popa

    February 2, 2019 at 12:45 pm

    I happen to have this function in my library of useful functions;
    function getFolderByName(folderName) {
    var myProject = app.project;
    for (var i = 1; i <= myProject.numItems; i++) {
    if ((myProject.item(i) instanceof FolderItem) && (myProject.item(i).name == folderName)){
    return myProject.item(i);
    }
    }
    myFolder = myProject.items.addFolder(folderName);
    return myFolder;
    }

    use it as myFolder = getFolderByName("CNM");

    Andrei
    My Envato portfolio.

  • Avinash Ramanath

    February 2, 2019 at 4:14 pm

    Hi,
    Sorry if I have been unclear. Here’s what am trying to do.

    1. create a folder called CNM
    2. iterate trough project items
    3. if a comp with name CNM_ found, then move it to folder CNM

    but every time I run the script below, folder CNM is duplicated

    I don’t want to duplicate if CNM is already present.

    //app.project.items.addFolder("CNM");
    for (var i = 1; i &lt;= app.project.numItems; i++) {
    var cItem = app.project.item(i);
    if ((cItem instanceof FolderItem) && (cItem.name == "CNM")) { var fCompositions = cItem; }
    }

    function getCompositions() {
    var allComps = new Array();
    if (app.project != null) {
    var items = app.project.items;
    for (var i = 1; i &lt;= items.length; i++)
    if ((items[i] instanceof CompItem) && (items[i].name.substring(0, 4) === "CNM_")) {
    allComps[allComps.length] = items[i];
    }
    }
    return allComps;
    }
    var comps = getCompositions();
    for (var i = 0; i &lt; comps.length; i++) {
    comp = comps[i];
    comp.parentFolder = fCompositions;
    }

  • Andrei Popa

    February 3, 2019 at 10:58 am

    Ok. So here is how i would do this. Haven’t tested it because i am not at the office. But i think this should work. As an advice, i think you should write your main code at the start and the functions at the end. That way, you can easily see what your code tries to do before going into depths.

    var fCompositions = getFolderByName("CNM");
    var comps = getCompositions(app.project);
    for (var i=0; i< comps.length;i++){
    comps[i].parentFolder=fCompositions;
    }

    function getCompositions(myProject){
    if (myProject == null) return;
    var allComps=[];
    for (var i=1; i<=myProject.numItems; i++){
    var currentComp = myProject.item(i);
    if (currentComp instanceof CompItem && currentComp.name.substring(0,4)=="CNM_"){
    allComps.push(currentComp);
    }
    }
    return allComps;
    }

    function getFolderByName(folderName) {
    if (app.project == null) return;
    var myProject = app.project;
    for (var i = 1; i <= myProject.numItems; i++) {
    if ((myProject.item(i) instanceof FolderItem) && (myProject.item(i).name == folderName)){
    return myProject.item(i);
    }
    }
    myFolder = myProject.items.addFolder(folderName);
    return myFolder;
    }

    Andrei
    My Envato portfolio.

  • Avinash Ramanath

    February 4, 2019 at 3:15 am

    Thanks, Andrei, this works. I will consider your advice. Thanks for taking the time out to solve my problem.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy