Copy link to clipboard
Copied
So I am trying to get a handle on getFiles and folder structure.
Currently I have a script that works fine, except it runs repetitively due to how I have my folder call setup. I'm uncertain how to get past it, because it has some functionality I need and frankly I'm new at using .js on folder structures.
So I have a target folder, tFolder, and within this, subfolders to run through the script. Within these folders are .psd files that have a count of 3-5 files.
Currently the script iterates through each file, however many files are in the subfolder, which of course isn't ideal. Could someone help with a modification of the script below? .pop seemed to be the right direction but I couldn't get it to work in this case.
try{
var tFolder = Folder.selectDialog("Select the folder with PSDs to process");
var mainFldr = tFolder.path;
}
catch(error){
alert("User Cancelled Folder Selection")
}
try {
if (tFolder != null) processFolder(tFolder);
function processFolder(folder) {
var fileList = folder.getFiles()
for (var i = 0; i < fileList.length; i++) {
if (fileList instanceof File && fileList.name.match(/\.(psd)$/i)) {
fileOpen(fileList, i);
}
////// This is the area I know I need to rewrite, but I cannot figure out a way to modify this correctly//////////
else if (fileList instanceof Folder) {
processFolder(fileList);
}
}
}
}
catch(error){
alert(String("Error in parent process \n"+error))
}
Copy link to clipboard
Copied
This is how I would normally do it...
var folders =[];
var topLevel = Folder.selectDialog("Please select top level folder");
folders = FindAllFolders(topLevel, folders);
folders.unshift(topLevel);
for(var a in folders){
var fileList = folders.getFiles("*.psd");
for(var z in fileList){
//open, process etc...
}
}
function FindAllFolders( srcFolderStr, destArray) {
var fileFolderArray = Folder( srcFolderStr ).getFiles();
for ( var i = 0; i < fileFolderArray.length; i++ ) {
var fileFoldObj = fileFolderArray;
if ( fileFoldObj instanceof File ) {
} else {
destArray.push( Folder(fileFoldObj) );
FindAllFolders( fileFoldObj.toString(), destArray );
}
}
return destArray;
};
Copy link to clipboard
Copied
Hi Paul,
This was really useful, In addition
Could you please suggest for saving the files in different folder.retaining the same structure of the input folder
For ex - var inputFolder = Folder ( 'D:/Work/In/');
Sub folders may be 1,2,,3
Saving the files in
var outputFolder = Folder( ['D:/Work/out/'] ); with same Sub folders may be 1,2,,3
Regards,
Vinoth
Copy link to clipboard
Copied
These might help as both support keeping folder structure...
Image Processor Pro
http://russellbrown.com/scripts.html
Picture Processor
Copy link to clipboard
Copied
Is this what you're looking for?
var rootFold = Folder(selectDialog());
var newFold = Folder(selectDialog());
var folders,fileList = [];
folders.push(rootFold);
getFileAndFold();
processFiles();
function getFileAndFold() {
for(var i=0;i<folders.length;i++) {
var files = folders.getFiles();
while(files.length > 0) {
for(var f in files) {
if(files
instanceof Folder) { folders.push(files
); } else if(files
.name.match(/\.(psd)$/i)) { fileList.push(files
); }
}
}
}
}
function processFiles() {
for(var fo in folders) {
var current = new Folder(folders[fo].replace(rootFold, newFold));
if(!current.exists) {
current.create();
}
}
for(var fi in fileList) {
//Perform your processing
var saveFile = new File(fileList[fi].replace(rootFold, newFold));
//setup saveOptions
activeDocument.saveAs(saveFile, saveOptions, true, Extension.LOWERCASE);
}
}
I apologize if parts may not work, I haven't had time to actually test it. Also, I may have the .replace() wrong; been working in Java a lot and it seems a little different in how it works.