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 amI 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 amWhat 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 pmI 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 pmHi,
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 CNMbut 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 <= 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 <= 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 < comps.length; i++) {
comp = comps[i];
comp.parentFolder = fCompositions;
} -
Andrei Popa
February 3, 2019 at 10:58 amOk. 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 amThanks, Andrei, this works. I will consider your advice. Thanks for taking the time out to solve my problem.
Reply to this Discussion! Login or Sign Up