ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Check if folder exists

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Avinash Ramanath
Check if folder exists
on Feb 2, 2019 at 2:05:20 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 &lt;= 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");
}


Return to posts index

Oleg Pirogov
Re: Check if folder exists
on Feb 2, 2019 at 10:09:48 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;


Return to posts index

Andrei Popa
Re: Check if folder exists
on Feb 2, 2019 at 12:45:52 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.


Return to posts index


Avinash Ramanath
Re: Check if folder exists
on Feb 2, 2019 at 4:14:18 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;
}


Return to posts index

Andrei Popa
Re: Check if folder exists
on Feb 3, 2019 at 10:58:08 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.


Return to posts index

Avinash Ramanath
Re: Check if folder exists
on Feb 4, 2019 at 3:15:18 am

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


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]